1.互斥锁
#include <myhead.h>
//定義全局變量
int num=100;
//定義互斥鎖
pthread_mutex_t mutex;
//定義線程體1
void *task1(void *arg)
{
while(1)
{
//申請鎖資源
pthread_mutex_lock(&mutex);
if(num<=0)
{
printf("沒有餘額了1\n");
pthread_mutex_unlock(&mutex);
break;
}
num-=10;
printf("張三取了10元還剩%d元\n",num);
//釋放鎖資源
pthread_mutex_unlock(&mutex);
sleep(1);
}
pthread_exit(NULL);
}
//定義線程體2
void *task2(void *arg)
{
while(1)
{
//申請鎖資源
pthread_mutex_lock(&mutex);
if(num<=0)
{
printf("沒有餘額了2\n");
pthread_mutex_unlock(&mutex);
break;
}
num-=20;
printf("李四取了20元還剩%d元\n",num);
//釋放鎖資源
pthread_mutex_unlock(&mutex);
sleep(2);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
//定義線程號
pthread_t tid1,tid2;
//創建線程
if(pthread_create(&tid1,NULL,task1,NULL))
{
perror("create tid1 error");
return -1;
}
if(pthread_create(&tid2,NULL,task2,NULL))
{
perror("create tid2 error");
return -1;
}
//初始化互斥鎖
pthread_mutex_init(&mutex,NULL);
//回收線程資源
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
//銷燬互斥鎖
pthread_mutex_destroy(&mutex);
return 0;
}
2.信号量
#include <myhead.h>
//定義信號量
sem_t sem;
//定義生產者
void *task1(void *arg)
{
while(1)
{
sleep(1);
printf("我生產了一架飛機\n");
//釋放資源
sem_post(&sem);
}
}
//定義消費者
void *task2(void *arg)
{
while(1)
{
sleep(2);
sem_wait(&sem);
printf("我購買了一架飛機\n");
}
}
int main(int argc, const char *argv[])
{
//定義線程號
pthread_t tid1,tid2;
//創建線程
if(pthread_create(&tid1,NULL,task1,NULL))
{
perror("create tid1 error");
return -1;
}
if(pthread_create(&tid2,NULL,task2,NULL))
{
perror("create tid2 error");
return -1;
}
//初始化信號量
sem_init(&sem,0,0);
//回收線程
pthread_join(tid1,NULL);
pthread_join(tid1,NULL);
//銷燬信號量
sem_destroy(&sem);
return 0;
}
3.条件变量
#include <myhead.h>
//定義互斥鎖
pthread_mutex_t mutex;
//定義條件變量
pthread_cond_t cond;
//創建生產者
void *task1(void *arg)
{
while(1)
{
sleep(2);
printf("這是一個生產者進程\n");
//喚醒隊列所有等待進程
pthread_cond_broadcast(&cond);
}
}
//創建消費者
void *task2(void *arg)
{
while(1)
{
//上鎖
pthread_mutex_lock(&mutex);
//入隊等待
pthread_cond_wait(&cond,&mutex);
printf("這是一個消費者進程1\n");
//解鎖
pthread_mutex_unlock(&mutex);
}
}
//創建消費者
void *task3(void *arg)
{
while(1)
{
sleep(2);
//上鎖
pthread_mutex_lock(&mutex);
//入隊等待
pthread_cond_wait(&cond,&mutex);
printf("這是一個消費者進程2\n");
//解鎖
pthread_mutex_unlock(&mutex);
}
}
//創建消費者
void *task4(void *arg)
{
while(1)
{
sleep(2);
//上鎖
pthread_mutex_lock(&mutex);
//入隊等待
pthread_cond_wait(&cond,&mutex);
printf("這是一個消費者進程3\n");
//解鎖
pthread_mutex_unlock(&mutex);
}
}
int main(int argc, const char *argv[])
{
//定義線程號
pthread_t tid1,tid2,tid3,tid4;
//創建線程
if(pthread_create(&tid1,NULL,task1,NULL))
{
perror("create tid1 error");
return -1;
}
if(pthread_create(&tid2,NULL,task2,NULL))
{
perror("create tid2 error");
return -1;
}
if(pthread_create(&tid3,NULL,task3,NULL))
{
perror("create tid3 error");
return -1;
}
if(pthread_create(&tid4,NULL,task4,NULL))
{
perror("create tid4 error");
return -1;
}
//初始化互斥鎖
pthread_mutex_init(&mutex,NULL);
//初始化條件變量
pthread_cond_init(&cond,NULL);
//回收線程資源
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
pthread_join(tid3,NULL);
pthread_join(tid4,NULL);
//銷燬互斥鎖
pthread_mutex_destroy(&mutex);
//銷燬條件變量
pthread_cond_destroy(&cond);
return 0;
}