- 论坛徽章:
- 0
|
本人QQ629638,望交前辈指导,感激不尽
#include<pthread.h>
#include"apue.h"
struct msg
{
struct msg *m_next;
int a;
};
struct msg *workq;
pthread_cond_t qready = PTHREAD_COND_INITIALIZER;
pthread_mutex_t qlock = PTHREAD_MUTEX_INITIALIZER;
void *process_msg()
{
struct msg *mp;
for(;
{
pthread_mutex_lock(&qlock);
while(workq == NULL)
{
printf("1\n" ;
pthread_cond_wait(&qready,&qlock);
printf("2\n" ;
}
printf("7\n" ;
mp=workq;
workq=mp->m_next;
pthread_mutex_unlock(&qlock);
}
}
void *enqueue_msg(struct msg *mp)
{
printf("3\n" ;
pthread_mutex_lock(&qlock);
mp->m_next=workq;
workq=mp;
printf("4\n" ;
pthread_mutex_unlock(&qlock);
printf("5\n" ;
pthread_cond_signal(&qready);
printf("6\n" ;
}
int main()
{
int err;
void **errt;
pthread_t tid1,tid2;
struct msg work;
err=pthread_create(&tid1,NULL,process_msg,(void *)1);
if(err!=0)
err_quit("can't create thread 1: %s\n",strerror(err));
err=pthread_create(&tid2,NULL,enqueue_msg,(void *)&work);
if(err!=0)
err_quit("can't create thread 2: %s\n",strerror(err));
sleep(5);
}
|
|