Implement a blocking counter with a reader/writer lock I'm trying to implement a blocking counter that may have several readers and writers at the same time:

class BlockingCounter {
public:
    BlockingCounter(int count) : count_(count) {
        // init lock_;
    }
    ~BlockingCounter() {
        // destroy lock_;
    }

    void Increment() {
        pthread_rwlock_wrlock(&amp;lock_);
        count_++;
        pthread_rwlock_unlock(&amp;lock_);

    }

    void Decrement() {
        pthread_rwlock_wrlock(&amp;lock_);
        count_--;
        pthread_rwlock_unlock(&amp;lock_);

    }

    int GetCount() {
        pthread_rwlock_rdlock(&amp;lock_);
        int count = count_;
        pthread_rwlock_unlock(&amp;lock_);
        return count;
    }

private:
    pthread_rwlock_t lock_;
    int count_;

};


Does this code have problems under multi-threading circumstances?

I intended to use it like this:

Main thread:

BlockingCounter *counter = new BlockingCounter(10);

// Here 10 threads are started. See code below.
while (counter-&gt;GetCount() &gt; 0) {
  ConditionVar.wait();
}
// Main thread resumes execution.


Other 10 thread(s):

// Do stuff
counter.Decrement();
ConditionVar.notify();
You are really writing C
C++ has its own language specific features to handle multi threading.
Your interface is broken.
The only way to test the value of the counter is via GetCount() but by the time you can use the value the counter may have changed.
 Thread1                      Thread2                    Thread3
 if (b.GetCount() == 0) {     if (b.GetCount() == 0) {   if (b.GetCount() == 0) {
     b.Increment();                b.Increment();             b.Increment();
     // I have the lock.
     Do stuff                      Do stuff                   Do stuff
 }                            }                          }

As you can see your mechanism does not stop all three threads from doing something. The only real use for the counter is to print the count (and even that may not be accurate but at least it wont break anything).