1.定义互斥锁
#include<myhead.h>int num=520;//临界资源//1.创建一个互斥锁变量
pthread_mutex_t mutex;//定义任务1函数
void *task1(void *arg)
{printf("11111111111111\n");//3.获取锁资源pthread_mutex_lock(&mutex);num=1314;sleep(3);printf("task1:num=%d\n",num);//释放锁资源pthread_mutex_unlock(&mutex);
}//定义任务2函数
void *task2(void *arg)
{printf("22222222222222\n");//3.获取锁资源pthread_mutex_lock(&mutex);num++;sleep(1);//在休眠时,任务1执行到赋值语句printf("task2:num=%d\n",num);//释放锁资源pthread_mutex_unlock(&mutex);
}
int main(int argc, const char *argv[])
{//2.初始化互斥锁pthread_mutex_init(&mutex,NULL);//创建两个线程pthread_t tid1,tid2;if(pthread_create(&tid1,NULL,task1,NULL)!=0){printf("tid1 create error\n");return -1;}if(pthread_create(&tid2,NULL,task2,NULL)!=0){printf("tid2 create error\n");return -1;}printf("tid1:%#lx,tid2:%#lx\n",tid1,tid2);//回收线程资源pthread_join(tid1,NULL);pthread_join(tid2,NULL);//5.销毁锁资源pthread_mutex_destroy(&mutex);return 0;
}
2.定义无名信号量
#include<myhead.h>
//1.创建无名信号量
sem_t sem;//定义生产者线程
void *task1(void *arg)
{int num=5;while(num--){sleep(1);printf("我生产了一辆特斯拉\n");//4.释放资源sem_post(&sem);}pthread_exit(NULL);//退出线程
}//定义消费者线程
void *task2(void *arg)
{int num=5;while(num--){//3.申请资源sem_wait(&sem);printf("我消费了一辆特斯拉\n");}pthread_exit(NULL);//退出线程
}
int main(int argc, const char *argv[])
{//2.初始化无名信号量sem_init(&sem,0,0);//第一个0表示用于线程同步,第二个0表示初始资源为0//创建两个线程,分别是生产者和消费者pthread_t tid1,tid2;if(pthread_create(&tid1,NULL,task1,NULL)!=0){printf("tid1 create error\n");return -1;}if(pthread_create(&tid2,NULL,task2,NULL)!=0){printf("tid2 create error\n");return -1;}printf("tid1:%#lx,tid2:%#lx\n",tid1,tid2);//回收线程资源pthread_join(tid1,NULL);pthread_join(tid2,NULL);//释放无名信号量sem_destroy(&sem);return 0;
}
3.创建三个线程,线程1打印输出字符A,线程2打印输出字符B,线程3打印输出字符C,用无名信号量输出结果为ABCABCABCABCABC。
#include<myhead.h>
//创建无名信号量
sem_t sem1,sem2,sem3;//定义线程1
void *task1(void *arg)
{int num=5;while(num--){sem_wait(&sem1);sleep(1);printf("A");fflush(stdout);sem_post(&sem2);}pthread_exit(NULL);
}
//定义线程2
void *task2(void *arg)
{int num=5;while(num--){sem_wait(&sem2);sleep(1);printf("B");fflush(stdout);sem_post(&sem3);}pthread_exit(NULL);
}
//定义线程3
void *task3(void *arg)
{int num=5;while(num--){sem_wait(&sem3);sleep(1);printf("C");fflush(stdout);sem_post(&sem1);}pthread_exit(NULL);
}int main(int argc, const char *argv[])
{sem_init(&sem1,0,1);sem_init(&sem2,0,0);sem_init(&sem3,0,0);pthread_t tid1,tid2,tid3;if(pthread_create(&tid1,NULL,task1,NULL)!=0){printf("tid1 create error\n");return -1;}if(pthread_create(&tid2,NULL,task2,NULL)!=0){printf("tid2 create error\n");return -1;}if(pthread_create(&tid3,NULL,task3,NULL)!=0){printf("tid3 create error\n");return -1;}printf("tid1:%#lx,tid2:%#lx,tid3:%#lx\n",tid1,tid2,tid3);pthread_join(tid1,NULL);pthread_join(tid2,NULL);pthread_join(tid3,NULL);sem_destroy(&sem1);sem_destroy(&sem2);sem_destroy(&sem3);puts("");return 0;
}
4.线程同步之条件变量
#include<myhead.h>
//1.定义条件变量
pthread_cond_t cond;//11.定义互斥锁变量
pthread_mutex_t mutex;//定义生产者线程1
void *task1(void *arg)
{int num=5;while(num--){sleep(1);printf("%#lx:生产了一辆特斯拉\n",pthread_self());//3.唤醒一个消费者//pthread_cond_signal(&cond);}//3.唤醒所有的等待线程pthread_cond_broadcast(&cond);//退出线程pthread_exit(NULL);
}
//定义消费者线程2
void *task2(void *arg)
{//33.上锁pthread_mutex_lock(&mutex);//4.进入等待队列pthread_cond_wait(&cond,&mutex);printf("%#lx:消费了一辆特斯拉\n",pthread_self());//44.解锁pthread_mutex_unlock(&mutex);//退出线程pthread_exit(NULL);
}
//定义消费者线程3
void *task3(void *arg)
{//33.上锁pthread_mutex_lock(&mutex);//4.进入等待队列pthread_cond_wait(&cond,&mutex);printf("%#lx:消费了一辆特斯拉\n",pthread_self());//44.解锁pthread_mutex_unlock(&mutex);//退出线程pthread_exit(NULL);
}
//定义消费者线程4
void *task4(void *arg)
{//33.上锁pthread_mutex_lock(&mutex);//4.进入等待队列pthread_cond_wait(&cond,&mutex);printf("%#lx:消费了一辆特斯拉\n",pthread_self());//44.解锁pthread_mutex_unlock(&mutex);//退出线程pthread_exit(NULL);
}
//定义消费者线程5
void *task5(void *arg)
{//33.上锁pthread_mutex_lock(&mutex);//4.进入等待队列pthread_cond_wait(&cond,&mutex);printf("%#lx:消费了一辆特斯拉\n",pthread_self());//44.解锁pthread_mutex_unlock(&mutex);//退出线程pthread_exit(NULL);
}
//定义消费者线程6
void *task6(void *arg)
{//33.上锁pthread_mutex_lock(&mutex);//4.进入等待队列pthread_cond_wait(&cond,&mutex);printf("%#lx:消费了一辆特斯拉\n",pthread_self());//44.解锁pthread_mutex_unlock(&mutex);//退出线程pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{//2.初始化条件变量pthread_cond_init(&cond,NULL);//22.初始化互斥锁pthread_mutex_init(&mutex,NULL);//创建六个线程,一个生产者,五个消费者pthread_t tid1,tid2,tid3,tid4,tid5,tid6;if(pthread_create(&tid1,NULL,task1,NULL)!=0){printf("tid1 create error\n");return -1;}if(pthread_create(&tid2,NULL,task2,NULL)!=0){printf("tid2 create error\n");return -1;} if(pthread_create(&tid3,NULL,task3,NULL)!=0){printf("tid3 create error\n");return -1;}if(pthread_create(&tid4,NULL,task4,NULL)!=0){printf("tid4 create error\n");return -1;}if(pthread_create(&tid5,NULL,task5,NULL)!=0){printf("tid5 create error\n");return -1;}if(pthread_create(&tid6,NULL,task6,NULL)!=0){printf("tid6 create error\n");return -1;}printf("tid1:%#lx,tid2:%#lx,tid3:%#lx,tid4:%#lx,tid5:%#lx5,tid6:%#lx\n",tid1,tid2,tid3,tid4,tid5,tid6);//回收线程资源pthread_join(tid1,NULL);pthread_join(tid2,NULL);pthread_join(tid3,NULL);pthread_join(tid4,NULL);pthread_join(tid5,NULL);pthread_join(tid6,NULL);//5.销毁条件变量pthread_cond_destroy(&cond);//55.销毁互斥锁pthread_mutex_destroy(&mutex);return 0;
}