多线程互斥
#include <myhead.h>
int num=1000;
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
/*void *task2(void *arg)
{
while(1)
{
num=num-50;
printf("李四取钱50后的%d\n",num);
sleep(1);
}
pthread_exit(NULL);
}*/
void *task2(void *arg)
{
while(1)
{
//上锁
pthread_mutex_lock(&mutex);
num=num-50;
printf("李四取钱50后的%d\n",num);
if(num<=0)
{
printf("无法取钱,钱不够了\n");
pthread_mutex_unlock(&mutex);
break;
}
sleep(1);
//释放锁资源
pthread_mutex_unlock(&mutex);
sleep(1);
}
pthread_exit(NULL);
}
void *task1(void *arg)
{
while(1)
{
//上锁
pthread_mutex_lock(&mutex);
num=num-100;
printf("张三取钱100后的%d\n",num);
if(num<=0)
{
printf("无法取钱,钱不够了\n");
pthread_mutex_unlock(&mutex);
break;
}
sleep(1);
//释放锁资源
pthread_mutex_unlock(&mutex);
sleep(1);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
pthread_t tid1,tid2;
if(pthread_create(&tid1,NULL,task1,NULL))
{
perror("open error");
return -1;
}
if(pthread_create(&tid2,NULL,task2,NULL))
{
perror("open 2 error");
return -1;
}
//
pthread_mutex_init(&mutex,NULL);
//回收线程资源
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
//销毁锁资源
pthread_mutex_destroy(&mutex);
pthread_mutex_destroy(&mutex);
return 0;
}
无名信号量
#include <myhead.h>
//1.定义一个无名信号量
sem_t sem,sem1;
void *task(void *arg)
{
while(1)
{
sleep(2);
printf("A");
//4.释放资源
sem_post(&sem);
}
}
void *task2(void *arg)
{
while(1)
{
//3.消费者申请资源
sem_wait(&sem);
printf("B");
sleep(1);
sem_post(&sem1);
// printf("消费了一辆奔驰\n");
}
}
void *task3(void *arg)
{
while(1)
{
sem_wait(&sem1);
printf("C\n");
sleep(2);
}
}
int main(int argc, const char *argv[])
{
pthread_t tid1,tid2,tid3;
if(pthread_create(&tid1,NULL,task,NULL))
{
perror("creat tid1 failed\n");
return -1;
}
if(pthread_create(&tid2,NULL,task2,NULL))
{
perror("creat tid2 failed\n");
return -1;
}
if(pthread_create(&tid3,NULL,task3,NULL))
{
perror("creat tid3 failed");
return -1;
}
//2.初始化无名信号量
//第一个0表示该无名信号用于线程之间
//第二个0表示初始值为0
sem_init(&sem,0,0);
sem_init(&sem1,0,0);
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
pthread_join(tid3,NULL);
//5.销毁无名信号量
sem_destroy(&sem);
sem_destroy(&sem1);
return 0;
}
条件变量:
#include <myhead.h>
//1 定义条件变量
pthread_cond_t cond;
//1.1 定义一个互斥锁
pthread_mutex_t mutex;
//创建生产者线程
void *task(void *arg)
{
while(1)
{
sleep(3);
printf("生产三辆跑车\n");
//3.释放资源
pthread_cond_broadcast(&cond);
}
}
//创建消费者线程
void *task2(void *arg)
{
while(1)
{
//3.3上锁
pthread_mutex_lock(&mutex);
sleep(1);
//4.申请资源
pthread_cond_wait(&cond,&mutex);
printf("%#lx:消费一个跑车\n",pthread_self());
//4.4解锁
pthread_mutex_unlock(&mutex);
}
}
int main(int argc, const char *argv[])
{
//定义两个线程
pthread_t tid,tid2,tid3,tid4;
if(pthread_create(&tid,NULL,task,NULL))
{
perror("create tid failed");
return -1;
}
if(pthread_create(&tid2,NULL,task2,NULL))
{
perror("create tid2 failed");
return -1;
}
if(pthread_create(&tid3,NULL,task2,NULL))
{
perror("create tid2 failed");
return -1;
}
if(pthread_create(&tid4,NULL,task2,NULL))
{
perror("create tid2 failed");
return -1;
}
//2.初始化条件变量
pthread_cond_init(&cond,NULL);
//2.2初始化互斥锁
pthread_mutex_init(&mutex,NULL);
// printf("tid1= %#lx tid2 =%#lx tid3=%#lx tid4=%#lx\n");
pthread_join(tid,NULL);
pthread_join(tid2,NULL);
pthread_join(tid3,NULL);
pthread_join(tid4,NULL);
//5.释放条件变量
pthread_cond_destroy(&cond);
//5.5释放锁的资源
pthread_mutex_destroy(&mutex);
return 0;
}