I have made a Queue-class containing storage vectors and mutexes. To initialise the queue, a thread needs to be started. To make sure the thread is started correctly, the constructor waits for a signal. The thread function is a friend of the Queue-class. However, the signal is not registered when sent by the thread function. Why?
Queue::Queue()
{
(...)
pthread_mutex_init( &mutex_cond_init, NULL);
Q_ready = false;
(...)
pthread_create(&thread_ID, NULL, Queue_function, this);
pthread_mutex_lock(&mutex_cond_init);
while(!Q_ready)
{
cout << "waiting" << endl;
pthread_cond_wait(&cond_init,&mutex_cond_init);
cout << "got signal" << endl;
}
pthread_mutex_unlock(&mutex_cond_init);
cout << "Queue open." <<endl;
}
void * Queue_function (void*arg)
{
(...)
Queue * S = (Queue*) arg;
pthread_mutex_lock(&(*S).mutex_cond_init);
(*S).Q_ready = true;
pthread开发者_StackOverflow_cond_signal(&(*S).cond_init);
pthread_mutex_unlock(&(*S).mutex_cond_init);
(...)
}
any help would be appreciated
Have you initialised cond_init
? Your code only shows initialisation of mutex_cond_init
.
精彩评论