I am trying to implement a concurrent queue, and skeleton of implementation is as following:
struct queue
{
int try_pop()
{
pthread_mutex_lock(&mutex_);
int rt;
while( (rt = do_pop()) == -1)
{
pthread_cond_wait(&cond_,&mutex_);
}
pthread_mutex_unlock(&mutex_);
return rt;
}
bool push(int num)
{
pthread_mutex_lock(&mutex_);
push_impl(num);
#if 0
/*signal before unlock*/
pthread_cond_signal(&cond_);
pthread_mutex_unlock(&mutex_);
#else
/*signal after unlock*/
pthread_mutex_unlock(&mutex_);
pthread_cond_signal(&cond_);
#endif
return true;
}
// private function and data member
}
I can call pthread_cond_signal
before or after unlocking the mutex
.
My question is what is difference between net effect of two approaches?