0

I have this Stack class where I can push/pop unicode strings in a thred-safe fashion.

#pragma once
#include <string>
#include <mutex>
#include <condition_variable>
using namespace std;

class Stack
{
    private:
      wstring *stack;
      int dimension;
      int numElements;
      mutex m;
      condition_variable stackIsFull;

    public:
      Stack(int N) : dimension(N), numElements(0)
      {
        stack = new wstring[N];
      }

     ~Stack()
     {
       delete[] stack;
     }

     void push(wstring element)
     {
       unique_lock<mutex> ul(m);
       stackIsFull.wait(ul, [this]() { return !(numElements==dimension); });

       stack[numElements] = element;
       numElements++;
     } 

     wstring pop()
     {
       lock_guard<mutex> ul(m);
       if (numElements == 0)
         return L"The stack is empty!\n";
       else
       {
         numElements--;
         wstring data = stack[numElements];
         stackIsFull.notify_all();
         return data;
       }
     }

};

I do not understand two things:

  1. Is this an initialization syntax?

    Stack(int N) : dimension(N), numElements(0)
      {
    
  2. What does the [] represent in the following line:

    stackIsFull.wait(ul, [this]() { return !(numElements==dimension); });
    

I read the explanation of the wait function on the documentation but there is not reference for the predicate: to be precise, there is not syntax which explain me the usage of the predicate pred.

Giuseppe Canto
  • 159
  • 1
  • 1
  • 14

0 Answers0