1.线程互斥
2.线程同步之无名信号量
3.线程同步之条件变量
1.
#include <myhead.h>
pthread_mutex_t mutex; //创建互吃锁
int num =600;
void *task1(void*arg)//线程 妖 爾 氵 进入王者峡谷
{
while(1)
{
pthread_mutex_lock(&mutex);//樱岛麻衣退出游戏 樱岛麻衣进入游戏
if(num>0)
{
usleep(1000);
num--;
printf("%#lx张三买了一张票剩下%d张\n",pthread_self(),num);
}
else
{
printf("%#lx票卖完了\n",pthread_self());
pthread_mutex_unlock(&mutex);//净化
break;
}
pthread_mutex_unlock(&mutex);//水银
}
pthread_exit(NULL);
}
void *task2(void*arg)//线程 妖 爾 氵 进入王者峡谷
{
while(1)
{
pthread_mutex_lock(&mutex);//樱岛麻衣退出游戏 樱岛麻衣进入游戏
if(num>0)
{
usleep(1000);
num--;
printf("%#lx李四买了一张票剩下%d张\n",pthread_self(),num);
}
else
{
printf("%#lx票卖完了\n",pthread_self());
pthread_mutex_unlock(&mutex);//净化
break;
}
pthread_mutex_unlock(&mutex);//水银
}
pthread_exit(NULL);
}
void *task3(void*arg)//线程 妖 爾 氵 进入王者峡谷
{
while(1)
{
pthread_mutex_lock(&mutex);//樱岛麻衣退出游戏 樱岛麻衣进入游戏
if(num>0)
{
usleep(1000);
num--;
printf("%#lx王五买了一张票剩下%d张\n",pthread_self(),num);
}
else
{
printf("%#lx票卖完了\n",pthread_self());
pthread_mutex_unlock(&mutex);//净化
break;
}
pthread_mutex_unlock(&mutex);//水银
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
pthread_mutex_init(&mutex,NULL);//重置
pthread_t tid1,tid2,tid3;//选择你的英雄
if(pthread_create(&tid1,NULL,task1,NULL)!=0)//正在载入对局
{
printf("创建失败\n");
return -1;
}
if(pthread_create(&tid2,NULL,task2,NULL)!=0)
{
printf("创建失败\n");
return -1;
}
if(pthread_create(&tid3,NULL,task3,NULL)!=0)
{
printf("创建失败\n");
return -1;
}
pthread_join(tid1,NULL);//gg
pthread_join(tid2,NULL);
pthread_join(tid3,NULL);
pthread_mutex_destroy(&mutex);//report
return 0;
}
2.
void *task1(void*arg)//上 中 下
{
while(1)
{
sem_wait(&sem3);//开局50阳光 种植太阳花
sleep(1);
fflush(stdout);
printf("%#lx购买一辆汽车A",pthread_self());
sem_post(&sem1);//产出阳光
}
pthread_exit(NULL);
}
void *task2(void*arg)
{
while(1)
{
sem_wait(&sem1);//种植阳光菇
sleep(1);//夜晚
fflush(stdout);
printf("%#lx购买一辆汽车B",pthread_self());
sem_post(&sem2);//产太阳
}
pthread_exit(NULL);
}
void *task3(void*arg)
{
while(1)
{
sem_wait(&sem2);//种植姐妹花
sleep(1);
fflush(stdout);
printf("%#lx购买一辆汽车C",pthread_self());
sem_post(&sem3);//产阳光
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
sem_init(&sem1,0,0);//开局50阳光 装备全靠产
sem_init(&sem2,0,0);
sem_init(&sem3,0,1);
pthread_t tid1,tid2,tid3;
if(pthread_create(&tid1,NULL,task1,NULL)!=0)//选择你的植物
{
printf("创建失败\n");
return -1;
}
if(pthread_create(&tid2,NULL,task2,NULL)!=0)
{
printf("创建失败\n");
return -1;
}
if(pthread_create(&tid3,NULL,task3,NULL)!=0)
{
printf("创建失败\n");
return -1;
}
pthread_join(tid1,NULL);//僵尸吃掉了你的脑子
pthread_join(tid2,NULL);
pthread_join(tid3,NULL);
sem_destroy(&sem1);
sem_destroy(&sem2);
sem_destroy(&sem3);
return 0;
}
3.
#include <myhead.h>
pthread_cond_t cond;//创建条件变量
pthread_mutex_t mutex;//一个互斥锁
void *task1(void*arg)//生产者线程
{
int num=5;
while(num--)
{
sleep(1);
printf("生产了1辆车\n");
pthread_cond_signal(&cond);//唤醒消费者
}
/* sleep(3);
printf("生产了五辆车\n");唤醒所有消费者
pthread_cond_broadcast(&cond);
*/
pthread_exit(NULL);
}
void *task2(void*arg)
{
pthread_mutex_lock(&mutex);//锁住
pthread_cond_wait(&cond,&mutex);//排队
printf("%#lx购买了一辆汽车\n",pthread_self());
pthread_mutex_unlock(&mutex);//解锁
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
pthread_mutex_init(&mutex,NULL);//初始化锁
pthread_cond_init(&cond,NULL);//初始化条件变量
pthread_t tid1,tid2,tid3,tid4,tid5,tid6;//创建线程
if(pthread_create(&tid1,NULL,task1,NULL)!=0)
{
printf("创建失败\n");
return -1;
}
if(pthread_create(&tid2,NULL,task2,NULL)!=0)
{
printf("创建失败\n");
return -1;
}
if(pthread_create(&tid3,NULL,task2,NULL)!=0)
{
printf("创建失败\n");
return -1;
}
if(pthread_create(&tid4,NULL,task2,NULL)!=0)
{
printf("创建失败\n");
return -1;
}
if(pthread_create(&tid5,NULL,task2,NULL)!=0)
{
printf("创建失败\n");
return -1;
}
if(pthread_create(&tid6,NULL,task2,NULL)!=0)
{
printf("创建失败\n");
return -1;
}
pthread_join(tid1,NULL);//毁灭吧
pthread_join(tid2,NULL);
pthread_join(tid3,NULL);
pthread_join(tid4,NULL);
pthread_join(tid5,NULL);
pthread_join(tid6,NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}