从多处理器编程的艺术,为什么在调用`pthread_cond_signal`前调用`pthread_mutex_unlock`?
1 #include <pthread.h>
2 #define QSIZE 16
3 typedef struct {
4 int buf[QSIZE];
5 long head, tail;
6 pthread_mutex_t *mutex;
7 pthread_cond_t *notFull, *notEmpty;
8 } queue;
9 void queue_enq(queue* q, int item) {
10 // lock object
11 pthread_mutex_lock (q->mutex);
12 // wait while full
13 while (q->tail - q->head == QSIZE) {
14 pthread_cond_wait (q->notFull, q->mutex);
15 }
16 q->buf[q->tail % QSIZE] = item;
17 q->tail++;
18 // release lock
19 pthread_mutex_unlock (q->mutex);
20 // inform waiting dequeuers
21 pthread_cond_signal (q->notEmpty);
22 }
和高级编程在Unix环境
void
enqueue_msg(struct msg *mp)
{
pthread_mutex_lock(&qlock);
mp->m_next = workq;
workq = mp;
pthread_mutex_unlock(&qlock);
pthread_cond_signal(&qready);
}
为什么pthread_mutex_unlock
调用pthread_cond_signal
之前叫什么名字?
In Java, unlocking is called after calling signal. 那么,为什么不同?
谢谢。
我认为这是错误的。看看https://stackoverflow.com/questions/4544234/calling-pthread-cond-signal-without-locking-mutex –
@JerryJeremiah谢谢。在APUE中,有一个例子在发送信号之前也释放锁定。查看我的更新。 – Ben