互斥锁
#include <myhead.h>
int num = 5000; //定义全局变量
//定义互斥锁
pthread_mutex_t mutex;
/*
* function: 创建线程体1
* @param [ in]
* @param [out]
* @return
*/
void *task1(void *arg)
{
while (1)
{
//上锁
pthread_mutex_lock(&mutex);
if (num <= 0)
{
printf("张三取钱结束,钱不够\n");
//释放锁资源
pthread_mutex_unlock(&mutex);
break;
}
num -= 100;
printf("张三取钱100元,剩余%d\n",num);
//释放锁资源
pthread_mutex_unlock(&mutex);
sleep(1);
}
//退出线程
pthread_exit(NULL);
}
/*
* function: 创建线程体2
* @param [ in]
* @param [out]
* @return
*/
void *task2(void *arg)
{
while (1)
{
//上锁
pthread_mutex_lock(&mutex);
if (num <= 0)
{
printf("李四取钱结束,钱不够\n");
//释放锁资源
pthread_mutex_unlock(&mutex);
break;
}
num -= 50;
printf("李四取钱50元,剩余%d\n",num);
//释放锁资源
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("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;
}
无名信号量
#include <myhead.h>
//定义三个无名信号量
sem_t sem1,sem2,sem3;
/*
* function: 创建A线程体
* @param [ in]
* @param [out]
* @return
*/
void *task1(void *arg)
{
while (1)
{
sleep(1);
//申请资源
sem_wait(&sem3);
printf("A\n");
//释放资源
sem_post(&sem1);
}
}
/*
* function: 创建B线程体
* @param [ in]
* @param [out]
* @return
*/
void *task2(void *arg)
{
while (1)
{
sleep(1);
//申请资源
sem_wait(&sem1);
printf("BB\n");
//释放资源
sem_post(&sem2);
}
}
/*
* function: 创建C线程体
* @param [ in]
* @param [out]
* @return
*/
void *task3(void *arg)
{
while (1)
{
sleep(1);
//申请资源
sem_wait(&sem2);
printf("CCC\n");
//释放资源
sem_post(&sem3);
}
}
int main(int argc, const char *argv[])
{
//定义三个线程
pthread_t tid1,tid2,tid3;
//创建线程
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;
}
//初始化无名信号量
sem_init(&sem1,0,0);
sem_init(&sem2,0,0);
sem_init(&sem3,0,1);
//堵塞回收子线程
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
pthread_join(tid3,NULL);
//销毁无名信号量
sem_destroy(&sem1);
sem_destroy(&sem2);
sem_destroy(&sem3);
return 0;
}
条件变量
#include <myhead.h>
//定义条件变量
pthread_cond_t cond;
//定义互斥锁
pthread_mutex_t mutex;
/*
* function: 创建生产者线程
* @param [ in]
* @param [out]
* @return
*/
void *task1(void *arg)
{
while (1)
{
sleep(3);
printf("我生产了一辆车\n");
//释放资源
// pthread_cond_signal(&cond);
//释放资源,并唤醒所有等待线程
pthread_cond_broadcast(&cond);
}
}
/*
* function: 创建消费者线程
* @param [ in]
* @param [out]
* @return
*/
void *task2(void *arg)
{
while (1)
{
sleep(1);
//上锁
pthread_mutex_lock(&mutex);
//申请资源
pthread_cond_wait(&cond,&mutex);
printf("%#lx:买了一辆车\n",pthread_self());
//释放锁资源
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,task2,NULL))
{
perror("create tid3 error");
return -1;
}
if (pthread_create(&tid4,NULL,task2,NULL))
{
perror("create tid4 error");
return -1;
}
//输出线程号
printf("tid1 = %#lx\ntid2 = %#lx\ntid3 = %#lx\ntid4 = %#lx\n",\
tid1,tid2,tid3,tid4);
//初始化互斥锁
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_cond_destroy(&cond);
//销毁互斥锁
pthread_mutex_destroy(&mutex);
return 0;
}