condvar.c

来自「unix环境编程」· C语言 代码 · 共 37 行

C
37
字号
#include <pthread.h>struct msg {	struct msg *m_next;	/* ... more stuff here ... */};struct msg *workq;pthread_cond_t qready = PTHREAD_COND_INITIALIZER;pthread_mutex_t qlock = PTHREAD_MUTEX_INITIALIZER;voidprocess_msg(void){	struct msg *mp;	for (;;) {		pthread_mutex_lock(&qlock);		while (workq == NULL)			pthread_cond_wait(&qready, &qlock);		mp = workq;		workq = mp->m_next;		pthread_mutex_unlock(&qlock);		/* now process the message mp */	}}voidenqueue_msg(struct msg *mp){	pthread_mutex_lock(&qlock);	mp->m_next = workq;	workq = mp;	pthread_mutex_unlock(&qlock);	pthread_cond_signal(&qready);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?