笔记3 linux 多线程 条件变量+互斥锁

//cond lock
#include<stdio.h>
#include<unistd.h>
#include<pthread.h> struct test
{
char a[];
char b[];
char c[];
}yb = {"","",""}; static int j=; pthread_mutex_t mutex_1 = PTHREAD_MUTEX_INITIALIZER;;
pthread_cond_t cond_1; void Print1(struct test *arg)
{
while()
{
pthread_mutex_lock(&mutex_1);
pthread_cond_wait(&cond_1,&mutex_1); //等待满足某些要求的消息到达
printf("a=%s,b=%s,c=%s,d=%X,j=%d\n",arg->a,arg->b,arg->c,pthread_self(),j++);
pthread_mutex_unlock(&mutex_1);
pthread_exit((void *)j);
}
printf("Never Coming.\n");
} void Print2(void *arg1)
{
pthread_mutex_lock(&mutex_1);
printf("coming.\n");
pthread_mutex_unlock(&mutex_1);
pthread_cond_signal(&cond_1);
} int main()
{
pthread_t pid1,pid2,pid3,pid4;
void *set;
pthread_cond_init(&cond_1,NULL); pthread_create(&pid1,NULL,&Print1,&yb);
pthread_create(&pid2,NULL,&Print2,&yb);
pthread_create(&pid3,NULL,&Print1,&yb);
pthread_create(&pid4,NULL,&Print2,&yb); pthread_join(pid1,&set);
printf("pid1 exit coed %d\n",(int)set);
pthread_join(pid2,&set);
printf("pid2 exit coed %d\n",(int)set);
pthread_join(pid3,&set);
printf("pid3 exit coed %d\n",(int)set);
pthread_join(pid4,&set);
printf("pid4 exit coed %d\n",(int)set); sleep();
pthread_cond_destroy(&cond_1);
return ;
}
上一篇:js 弹层 提示


下一篇:dubbox 编译 和 测试