当前位置: 首页 > news >正文

系统编程:互斥锁,条件变量

互斥锁

使用过程:
1,声明锁: pthread_mutex_t lock;
2,初始化锁:pthread_mutex_init(&lock,NULL);
3,在线程的方法函数中上锁和解锁:(成对出现)
pthread_mutex_lock(&lock);
pthread_mutex_unlock(&lock);
4,销毁锁:pthread_mutex_destroy(&lock);

代码示例:

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>pthread_mutex_t lock;//在全局区声明互斥锁void *tick(void *arg)
{static int num =10;while(num>0){pthread_mutex_lock(&lock);if(num<=0){pthread_mutex_unlock(&lock);break;}num--;sleep(1);printf("线程%ld销售了一张票,还剩%d张\n",pthread_self(),num);pthread_mutex_unlock(&lock);}
}int main(int argc,char const *argv[])
{pthread_mutex_init(&lock,NULL);//初始化互斥锁pthread_t p1,p2,p3,p4; //定义四个线程;共享此进程的数据pthread_create(&p1,NULL,tick,NULL);pthread_create(&p2,NULL,tick,NULL);pthread_create(&p3,NULL,tick,NULL);pthread_create(&p4,NULL,tick,NULL);//创建四个线程pthread_join(p1,NULL);pthread_join(p2,NULL);pthread_join(p3,NULL);pthread_join(p4,NULL);//等待四个线程结束pthread_mutex_destroy(&lock);//销毁互斥锁return 0;
}

互斥锁+条件变量

条件变量:使用过程:
1,声明条件变量 pthread_cond_t cond;
2,初始化条件变量 pthread_cond_init(&cond,NULL);
3,在一对互斥锁中间:pthread_cond_wait(&cond,&lock);//会打开互斥锁,并且阻塞程序,–直到另一个信号函数pthread_cond_broadcast(&cond);被执行时解除阻塞.或者pthread_cond_signal(&cond)函数;
4,释放条件变量pthread_cond_destroy(&cond);

代码示例:生产者-消费者模型

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>pthread_mutex_t lock;//声明互斥锁
pthread_cond_t cond; //声明条件变量int num = 0; //声明记录库存数量的变量void *produce(void *argv)
{while(1){pthread_mutex_lock(&lock);while(num >= 10){printf("库存已满,线程%ld停止生产\n",pthread_self());pthread_cond_wait(&cond,&lock);}num++;printf("线程%ld生产了一个商品,当前库存数量为:%d\n",pthread_self(),num);pthread_cond_broadcast(&cond);pthread_mutex_unlock(&lock);sleep(1);}return NULL;
}void *sale(void *argv)
{while(1){pthread_mutex_lock(&lock);while(num <= 0){printf("库存为0,线程%ld停止销售\n",pthread_self());pthread_cond_wait(&cond,&lock);}num--;printf("线程%ld销售了一个商品,当前库存数量为:%d\n",pthread_self(),num);pthread_cond_broadcast(&cond);pthread_mutex_unlock(&lock);int t = rand()%5;sleep(t);}
}void closeThread(pthread_t ts[],int len) //声明释放线程的方法
{for(int i = 0; i < len; i++){pthread_join(ts[i],NULL);}
}int main(int argc, char const *argv[])
{srand(time(NULL));pthread_mutex_init(&lock,NULL);//初始化互斥锁pthread_cond_init(&cond,NULL);//初始化条件变量pthread_t ps[3];//声明生产者线程组pthread_t ss[5];//声明销售者线程组int i;for(i = 0; i < 3; i++){pthread_create(&ps[i],NULL,produce,NULL);//创建线程并执行}for(int i = 0; i < 5; i++){pthread_create(&ps[i],NULL,sale,NULL);}int plen = sizeof(ps)/sizeof(pthread_t);closeThread(ps,plen);//释放生产者线程int slen = sizeof(ss)/sizeof(pthread_t);closeThread(ss,slen);//释放消费者线程pthread_mutex_destroy(&lock);//释放互斥锁pthread_cond_destroy(&cond);//释放条件变量return 0;
}
http://www.lryc.cn/news/377783.html

相关文章:

  • 蓝鹏测控公司全长直线度算法项目多部门现场组织验收
  • 使用Python进行音频处理
  • 家有老人小孩,室内灰尘危害大!资深家政教你选对除尘空气净化器
  • AI在创造与毁灭之间摇摆:音乐产业的机遇与挑战并存
  • Spring Boot集成 Spring Retry 实现容错重试机制并附源码
  • MDK-ARM 编译后 MAP 文件分析
  • antv g6实现系统拓扑图
  • 因路径规划异常导致导航停止 Failed to pass global plan to the controller
  • AOSP开发环境搭建
  • React native新架构组成
  • Spring Security+Spring Boot实现登录认证以及权限认证
  • 5款堪称变态的AI神器,焊死在电脑上永不删除!
  • Python和OpenCV图像分块之图像边长缩小比率是2
  • C语言中的位域(bit-field)是什么,以及它的用途和优缺点
  • 从面试角度了解前端基础知识体系
  • 【DKN: Deep Knowledge-Aware Network for News Recommendation】
  • Linux管道与重定向
  • kotlin数组
  • SpringSecurity实战入门——认证
  • 23种设计模式之桥接模式
  • vuejs3+elementPlus后台管理系统,左侧菜单栏制作、跳转、默认激活菜单
  • 代码随想录算法训练营第四十四天|LeetCode198 打家劫舍、LeetCode213 打家劫舍Ⅱ
  • Git进阶使用(图文详解)
  • Effective C++ 改善程序与设计的55个具体做法笔记与心得 4
  • WordPress管理员后台登录地址修改教程,WordPress admin登录地址文件修改方法
  • Python基础教程(二十四):日期和时间
  • java面向对象(上)
  • 揭示SOCKS5代理服务器列表的重要性
  • 机器学习python实践——关于ward聚类分层算法的一些个人心得
  • 从零制作一个docker的镜像