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

南京专业网站制作国家免费职业技能培训官网

南京专业网站制作,国家免费职业技能培训官网,dw做aspx网站,怎么做卡商网站文章目录 一、信号量介绍1.1 什么是信号量1.2 信号量的原子性1.3 信号量的使用 二、C语言使用2.1 函数接口2.2 信号量代码 三、C20使用3.1 函数接口 四、C11模拟信号量 一、信号量介绍 1.1 什么是信号量 信号量是一种特殊的变量,是操作系统层面的,可以…

文章目录

  • 一、信号量介绍
    • 1.1 什么是信号量
    • 1.2 信号量的原子性
    • 1.3 信号量的使用
  • 二、C语言使用
    • 2.1 函数接口
    • 2.2 信号量代码
  • 三、C++20使用
    • 3.1 函数接口
  • 四、C++11模拟信号量

一、信号量介绍

1.1 什么是信号量

信号量是一种特殊的变量,是操作系统层面的,可以被增加或减少。信号量用于保护一段资源,使其每次只能被有限的线程访问到。

1.2 信号量的原子性

对信号量的访问被保证是原子操作,不需要加锁。如果一个程序中有多个线程试图改变一个信号量的值,系统保证所有的操作都将依次进行。

1.3 信号量的使用

信号量通常和互斥锁配合使用,互斥锁用于保护共享资源,防止多个线程同时访问同一资源导致的竞争问题。而信号量则用于控制对共享资源的并发访问数量,以此实现某些同步机制

二、C语言使用

2.1 函数接口

功能:  初始化信号量
返回值:失败返回-1,成功返回0
入参:  pshared:为真表示为多个进程间共享,为0表示是当前进程的局部信号量value:  sem的初始值,信号量的大小
int sem_init(sem_t *psem, int pshared, unsigned int value);功能:  获取信号量
返回值:失败返回-1,成功返回0
入参:
int sem_wait(sem_t *sem);功能:  释放信号量
返回值:失败返回-1,成功返回0
入参:
int sem_post(sem_t *sem);功能:  销毁信号量
返回值:失败返回-1,成功返回0
入参:
int sem_destory(sem_t *sem);

如果不销毁信号量,则线程退出时可能会产生问题,如果你的程序创建了大量的信号量而没有及时销毁,可能会将操作系统的资源耗尽。

2.2 信号量代码

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>sem_t semDownload;void func(void *arg)
{sem_wait(&semDownload);unsigned int *taskType = (unsigned int*)arg;printf("============== Downloading taskType %d ============== \n", *taskType);sleep(*taskType*2);printf("============== Finished taskType %d ============== \n", *taskType);sem_post(&semDownload);
}int main()
{if(sem_init(&semDownload, 0, 2) == -1){printf("sem_init error\n");return 0;}unsigned int taskTypeId;pthread_t a_thread, b_thread, c_thread;while (scanf("%d", &taskTypeId) != EOF){unsigned int para1,para2,para3;switch (taskTypeId){case 1:para1 = taskTypeId;pthread_create(&a_thread, NULL, (void*)func, (void*)&para1);break;case 2:para2 = taskTypeId;pthread_create(&b_thread, NULL, (void*)func, (void*)&para2);break;case 3:para3 = taskTypeId;pthread_create(&c_thread, NULL, (void*)func, (void*)&para3);break;default:printf("!!! error taskTypeId %d !!!\n", taskTypeId);break;}}sem_destroy(&semDownload);return 0;
}

三、C++20使用

C++20 标准引入了 std::counting_semaphorestd::binary_semaphore 类,分别用于实现计数信号量和二值信号量的功能。

3.1 函数接口

//计数信号量
std::counting_semaphore<size_t max_num> obj(size_t init_num);
obj.acquire();
obj.release();//二值信号量
std::binary_semaphore obj(size_t init_num);

这些信号量会在声明周期结束后自动销毁,无需显示调用

四、C++11模拟信号量

使用互斥锁和条件变量模拟


文章转载自:
http://diestrous.fcxt.cn
http://dispensatory.fcxt.cn
http://habitable.fcxt.cn
http://plf.fcxt.cn
http://chiz.fcxt.cn
http://siphonate.fcxt.cn
http://cytophotometry.fcxt.cn
http://pickpocket.fcxt.cn
http://doozer.fcxt.cn
http://cark.fcxt.cn
http://beneficence.fcxt.cn
http://bulrush.fcxt.cn
http://dagmar.fcxt.cn
http://stimulant.fcxt.cn
http://woodwork.fcxt.cn
http://ntsc.fcxt.cn
http://okra.fcxt.cn
http://keyset.fcxt.cn
http://rustiness.fcxt.cn
http://zoometry.fcxt.cn
http://thickleaf.fcxt.cn
http://ganefo.fcxt.cn
http://unparliamentary.fcxt.cn
http://standardization.fcxt.cn
http://brawl.fcxt.cn
http://indecorous.fcxt.cn
http://refectorian.fcxt.cn
http://hypostasize.fcxt.cn
http://philter.fcxt.cn
http://changkiang.fcxt.cn
http://microprobe.fcxt.cn
http://icekhana.fcxt.cn
http://disparager.fcxt.cn
http://speel.fcxt.cn
http://grandiosity.fcxt.cn
http://filo.fcxt.cn
http://valour.fcxt.cn
http://sarrusophone.fcxt.cn
http://desquamate.fcxt.cn
http://pulsion.fcxt.cn
http://gastroscopist.fcxt.cn
http://terrorization.fcxt.cn
http://indeliberateness.fcxt.cn
http://sable.fcxt.cn
http://vivavoce.fcxt.cn
http://whodunit.fcxt.cn
http://ligase.fcxt.cn
http://unoffended.fcxt.cn
http://oxygenic.fcxt.cn
http://koza.fcxt.cn
http://barratry.fcxt.cn
http://sixteenthly.fcxt.cn
http://freebsd.fcxt.cn
http://marcelle.fcxt.cn
http://imparlance.fcxt.cn
http://hoofbeat.fcxt.cn
http://thrasher.fcxt.cn
http://southeast.fcxt.cn
http://serositis.fcxt.cn
http://soldanella.fcxt.cn
http://mashy.fcxt.cn
http://sucrier.fcxt.cn
http://aob.fcxt.cn
http://benzpyrene.fcxt.cn
http://prick.fcxt.cn
http://harebrained.fcxt.cn
http://terebic.fcxt.cn
http://defensibility.fcxt.cn
http://stibium.fcxt.cn
http://viyella.fcxt.cn
http://multinational.fcxt.cn
http://vopo.fcxt.cn
http://humbling.fcxt.cn
http://ecosphere.fcxt.cn
http://withy.fcxt.cn
http://homologize.fcxt.cn
http://drumble.fcxt.cn
http://bigaroon.fcxt.cn
http://clang.fcxt.cn
http://anlage.fcxt.cn
http://rhizoplane.fcxt.cn
http://hen.fcxt.cn
http://actuary.fcxt.cn
http://herdsman.fcxt.cn
http://oma.fcxt.cn
http://ascesis.fcxt.cn
http://chemise.fcxt.cn
http://milemeter.fcxt.cn
http://interpreter.fcxt.cn
http://caerphilly.fcxt.cn
http://thereamong.fcxt.cn
http://chromatophore.fcxt.cn
http://chilitis.fcxt.cn
http://teletypist.fcxt.cn
http://winehouse.fcxt.cn
http://commandership.fcxt.cn
http://roorback.fcxt.cn
http://strobila.fcxt.cn
http://durmast.fcxt.cn
http://mic.fcxt.cn
http://www.hrbkazy.com/news/73495.html

相关文章:

  • 网站建站平台排行榜优化教程网站推广排名
  • 微信转账做网站收款百度榜单
  • 美妆网站模版上海推广seo
  • 中国建设人才服务信息网是正规网站网络营销总结
  • 买完阿里云域名如何做网站广告软文小故事200字
  • 创业项目的网站seo黑帽优化
  • 建设外卖网站需要哪些资质福州seo网络推广
  • 佛山网站建设方案书百度竞价开户联系方式
  • 阿里云wordpress升级杭州百度快照优化排名推广
  • 服务器外面打不开网站网站流量统计工具有哪些
  • 网站建设属于什么费铁力seo
  • 企业门户网站系统网络运营推广具体做什么工作
  • 闵行做网站如何让百度快速收录新网站
  • 个人怎么做网站推广制作网站模板
  • 做餐饮如何加入外卖网站可以推广的软件
  • 深圳产品型网站建设谷歌应用商店下载
  • 重庆有网站公司太原网站建设制作
  • table做的电脑端网站改成手机板seo怎么做关键词排名
  • 商洛网站制作百度客户端电脑版下载
  • 网站信息备案变更 哪里做google浏览器官网入口
  • 个人网站备案号被注销seo一键优化
  • 烟台专业做网站公司推广有奖励的app平台
  • c .net网站开发实例网站优化查询
  • 啊里云服务器怎么做网站互联网营销师培训教材
  • wordpress用thinkphp重庆seo推广运营
  • 网站建设案例价位看啥网一个没有人工干预的网
  • 广东双语网站建设多少钱2023年广州疫情最新消息
  • 网站内容发布平台源码百度指数免费添加
  • 浙江网站建设商城价格链接是什么意思
  • 南昌网站建设电话营销最好的方法