Skip to main content
deleted 25 characters in body
Source Link
shtkuh
  • 165
  • 5
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;
};
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 = -1;

        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;
};
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);
        T 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;
};
deleted 2 characters in body
Source Link
shtkuh
  • 165
  • 5
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(intT 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();
    }

    intТ get()
    {
        std::unique_lock<std::mutex> locker(mu);
        int top_value = -1;

        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;
};
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(int 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();
    }

    int get()
    {
        std::unique_lock<std::mutex> locker(mu);
        int top_value = -1;

        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;
};
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 = -1;

        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;
};
added 18 characters in body
Source Link
shtkuh
  • 165
  • 5
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(int 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();
    }

    int get()
    {
        std::unique_lock<std::mutex> locker(mu);
        int top_value = -1;

        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;
};
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 size_ - top_ - 1;
    }

    void set(int 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();
    }

    int get()
    {
        std::unique_lock<std::mutex> locker(mu);
        int top_value = -1;

        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;
};
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(int 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();
    }

    int get()
    {
        std::unique_lock<std::mutex> locker(mu);
        int top_value = -1;

        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;
};
Source Link
shtkuh
  • 165
  • 5
Loading