template <class T>
class Stack
{
public:
Stack(size_t size) : size_(size)
{
arr_ = new T[size_];
init();
}
~Stack()
{
delete[] arr_;
}
void init()
{
top_ = -1;
}
int capacity()
{
return static_cast<int>(size_) - top_ - 1;
}
void set(T num)
{
std::unique_lock<std::mutex> locker(mu);
cond.wait(locker, [this](){ return top_ < static_cast<int>(size_) - 1; });
top_++;
arr_[top_] = num;
std::cout << "Setting num: " << num << " Capacity: " << this->capacity() << std::endl;
locker.unlock();
cond.notify_one();
}
Т get()
{
std::unique_lock<std::mutex> locker(mu);
int top_value =T -1;top_value;
cond.wait(locker, [this](){ return top_ != -1; });
top_value = arr_[top_];
top_--;
std::cout << "Getting num: " << top_value << " Capacity: " << this->capacity() << std::endl;
locker.unlock();
cond.notify_one();
return top_value;
}
private:
size_t size_;
T* arr_;
int top_;
std::mutex mu;
std::condition_variable cond;
};