当前位置: 技术问答>linux和unix
使用条件变量和互斥量的例子,中间有些地方看不懂,求帮忙解释一下,
来源: 互联网 发布时间:2017-03-29
本文导语: #include struct msg{ struct msg *m_next; }; struct msg *workq; pthread_cond_t qready = PTHREAD_COND_INITIALIZER; pthread_mutex_t qlock = PTHREAD_MUTEX_INITIALIZER; void process_msg(void) { struct msg *mp; for(;;){ pthread_mutex_lock(&qlock); while(...
#include
struct msg{
struct msg *m_next;
};
struct msg *workq;
pthread_cond_t qready = PTHREAD_COND_INITIALIZER;
pthread_mutex_t qlock = PTHREAD_MUTEX_INITIALIZER;
void process_msg(void)
{
struct msg *mp;
for(;;){
pthread_mutex_lock(&qlock);
while(workq==NULL)
pthread_cond_wait(&qready,&qlock);
mp=workq;//????????????
workq=mp->m_next;//这里2行什么意思是用到队列了吗???
pthread_mutex_unlock(&qlock);
}
}
void enqueue_msg(struct msg *mp)
{
pthread_mutex_lock(&qlock);
mp->m_next = workq;//????????
workq = mp;//?????????????
pthread_mutex_unlock(&qlock);
pthread_cond_signal(&qready);
}
int main(void)
{
return 0;
}
struct msg{
struct msg *m_next;
};
struct msg *workq;
pthread_cond_t qready = PTHREAD_COND_INITIALIZER;
pthread_mutex_t qlock = PTHREAD_MUTEX_INITIALIZER;
void process_msg(void)
{
struct msg *mp;
for(;;){
pthread_mutex_lock(&qlock);
while(workq==NULL)
pthread_cond_wait(&qready,&qlock);
mp=workq;//????????????
workq=mp->m_next;//这里2行什么意思是用到队列了吗???
pthread_mutex_unlock(&qlock);
}
}
void enqueue_msg(struct msg *mp)
{
pthread_mutex_lock(&qlock);
mp->m_next = workq;//????????
workq = mp;//?????????????
pthread_mutex_unlock(&qlock);
pthread_cond_signal(&qready);
}
int main(void)
{
return 0;
}
|
看代码不就是消息的入队出队操作吗?