I was wondering if there is any way of implementing semaphores without using the help of inbuilt functions of c++ like mutex, condition variables ? I tried implementing like this
class Semaphore {
private:
int count;
public:
Semaphore (int count_ = 1)
{
this->count = count_;
}
inline void notify( int tid ) {
count++;
cout << "thread " << tid << " notify" << endl;
}
inline void wait( int tid ) {
while(count == 0) {
cout << "thread " << tid << " wait" << endl;
}
cout << "thread " << tid << " run" << endl;
count--;
}
};
But I don't think that these member functions are atomic and also producing wrong outputs. How can we improve this ? Thanks in advance