I have a question about muti threading synchronization.. We suppose that we have 2 pthread and an fifo queue. Thread 1 will insert the elements in this queue and thread 2 will extract these elements from the same queue. I implemented the two operations of my queue: push and pop.
void push(element e) {
pthread_mutex_lock(&mutex);
myVector.push_back(e);
pthread_cond_signal(&empty);
pthread_mutex_unlock(&mutex);
}
Element pop() {
pthread_mutex_lock(&mutex);
if(myVector.size() == 0)
pthread_cond_wait(&empty, &mutex);
//extract the element from the queue;
pthread_mutex_unlock(&mutex);
}
the thread2 will then have this life cycle:
while(myBoolFlag) {
Element theElement = myQueue->pop();
usleep(500000);
}
this code can lead to deadlock situations? before of wait, must I unlock the mutex?