【发布时间】:2014-05-08 22:00:27
【问题描述】:
考虑以下几点:
pthread_mutex_t m;
pthread_cond_t c;
//a bunch of passenger threads will all be doing this
pthread_mutex_lock(&m); //passengers join the queue for boarding one at a time
pthread_cond_wait(&c, &m); //joined queue, mutex now unlocked allowing other passengers to join queue
//wait for a ride thread to signal the variable (representing the queue), allowing passengers to board one at a time
//
//Do some work in here required for boarding the ride
//
pthread_mutex_unlock(&m); //allow the next passenger to do the work required to board
我能否保证一次只有一名乘客能够访问“在这里做一些工作”部分?据我了解,一旦条件变量发出信号,该线程将重新锁定互斥锁并继续。如果在一个线程收到信号并开始工作后,ride 线程再次向 cond 变量发出信号会发生什么?第二个线程会等待第一个线程解锁互斥锁,还是两个线程现在都在“在这里做一些工作”部分?
【问题讨论】: