C/C++ 使用信号量控制线程运行顺序

2023-05-16

#include <stdio.h>
#include <semaphore.h>  // semaphore/seməfɔːr/
#include <pthread.h>


//信号量的主要函数有:


/*
 * 函数名:sem_init()
 * 功能:对指定信号初始化
 * 参数1:*sem:信号,
 * 参数2:pshared=0时,信号在当前进程的多个线程之间共享
 * 参数3:unsigned
 * 参数4:value表示初始化信号的值
 * int sem_init(sem_t *sem,int pshared,unsigned int value);
 */


/*
 * 函数名:sem_wait()
 * 功能:阻塞当前进程,直到信号量的值大于0,解除阻塞,
 *      解除阻塞后sem的值-1表示公共资源执行减少了,例
 *      如:如果你对一个值为2的信号量调用sem_wait(),
 *      线程将会继续执行,信号量的值将-1。当初始化
 *      value=0后,使用sem_wai会阻塞这个线程,这个
 *      线程函数就会等待其它线程函数调用sem_post增加
 *      了了这个值使它不再是0,才开始执行,然后value值-1。
 * 参数:*sem
 * int sem_wait(sem_t *sem);
 */


/*
 * 函数名:sem_post(sem_t *sem);
 * 功能:增加信号量的值+1,当有线程阻塞在这个信号量上时,
 *      调用这个函数会使其中的一个线程不再阻塞,选择机制
 *      由线程的调度策略决定
 * 参数:*sem
 * int sem_post(sem_t *sem);
 */
sem_t sem,sem1,sem2;


void* thread_a(void* arg){
    LOOP:
    sem_wait(&sem2);
    printf("thread_a running \n");
    sem_post(&sem);
    sleep(1);
    goto LOOP;
}


void* thread_b(void* arg){
    LOOP:
    sem_wait(&sem);
    printf("thread_b running \n");
    sem_post(&sem1);
    sleep(1);
    goto LOOP;
}


void* thread_c(void* arg){
    LOOP:
    sem_wait(&sem1);
    printf("thread_c running \n");
    sem_post(&sem2);
    sleep(1);
    goto LOOP;
}


int main(void){
    sem_init(&sem,0,1);
    sem_init(&sem1,0,0);
    sem_init(&sem2,0,0);


    pthread_t id[2];


    pthread_create(&id[0],NULL,thread_a,NULL);
    pthread_create(&id[1],NULL,thread_b,NULL);
    pthread_create(&id[2],NULL,thread_c,NULL);


    pthread_join(id[0],NULL);
    pthread_join(id[1],NULL);
    pthread_join(id[2],NULL);


    return 0;
}
iotek@iotekclass:~/pwz1903C++/Depa/面试题/方凌计算机$ gcc test4.c -pthread
iotek@iotekclass:~/pwz1903C++/Depa/面试题/方凌计算机$ ./a.out
thread_b running 
thread_c running 
thread_a running 
thread_b running 
thread_c running 
thread_a running 
thread_b running 
thread_c running 
thread_a running 
thread_b running 
thread_c running 
thread_a running 
thread_b running 
thread_c running 
thread_a running 
thread_b running 

解释来自https://blog.csdn.net/u011124985/article/details/79994293

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C/C++ 使用信号量控制线程运行顺序 的相关文章

  • PX4飞控学习(四)

    系统启动 启动文件 xff1a nuttx arch arm stm32 stm32 start c stm32 clockconfig span class hljs regexp span 时钟 stm32 fpuconfig span
  • VSCode 搭建 C++ 开发环境

    文章目录 前言一 获取参考资料二 下载安装 VSCode三 安装编译器四 添加环境变量五 使用VSCode 开发 C 43 43 程序总结 前言 鲁迅曾说过 xff0c 不以敲代码为目的的学编程都是耍流氓 xff01 我最近在撸 C 43
  • Ubuntu安装VMware

    Ubuntu安装VMware xff08 1 xff09 需求 由于windows 的日渐卡顿还有变态的更新 xff0c 我的需求就是稳定单调优化好所以我通过Ubuntu 安装VMware xff0c 然后开启虚拟机继续学习 xff08 2
  • python实现TCP通信

    本例是在Ubuntu虚拟机中本机互传实现的TCP通信 一 TCP服务器端 xff08 server端 xff09 1 创建套接字 xff0c 绑定套接字到本地IP与端口 s 61 socket socket socket AF INET s
  • agrc argv解释

    以前经常看见过 xff1a int main int argc char argv 这样形式的main但是一直没有这样用直到研究点云时发现有个例子是 xff1a gt exe pcd 这样的doc下的命令才想起有这样的两个参数 xff0c
  • 个人面试细节、技巧总结(没有面试题哦!)

    面试除了自身技能过硬外 xff0c 良好的沟通 xff0c 平和的心态 xff0c 细节的拿捏也都是额外的加分项 最后 xff0c 以些许运气加以点缀 xff0c offer 便八九不离十了 参加工作两年有余 xff0c 只大专文凭 xff
  • 【记录】ORB-SLAM3编译以及在realsense D435i运行

    环境 xff1a 最开始用的是源码是ORB SLAM3 的1 0版本 xff0c 但是编译的时候出错太多了 xff0c 超出了能力范围 xff0c 更换了0 4 beta版本 xff0c 但是这个版本在运行的时候会直接segmentatio
  • ArtiPub

    ArtiPub ArtiPub Article Publisher的简称 xff0c 意为 34 文章发布者 34 是一款开源的一文多发平台 xff0c 可以帮助文章作者将编写好的文章自动发布到掘金 SegmentFault CSDN 知乎
  • mac安装配置zsh

    mac安装配置zsh 比mac自带的shell好用太多 一 安装homebrew 参考 xff1a https brew sh index zh cn bin bash c span class token string 34 span c
  • 手把手教你给win10 2004版本的ubuntu1804子系统安装docker

    ubuntu1804子系统安装docker ce 分两种情况 xff1a 1 win10版本小于2004版本2 win10版本大于2004版本 一 说明 win10版本小于2004的话 xff0c 可以使用WSL1 0 xff0c WSL1
  • windows安装scoop

    参考 xff1a https scoop sh 参考 xff1a https github com lukesampson scoop wiki Quick Start 懂不懂 xff0c 先装上 xff0c 这样你就完成了该工具学习的第一
  • PX4飞控学习(五)

    PX4的应用 程序入口为 程序名 main int argc char argv 这里实现应用参数 主循环函数在 task main int argc char argv thread main int argc char argv 等函数
  • 转载kubernetes 1.9 与 CentOS 7.3 内核兼容问题

    20201022转载kubernetes 1 9 与 CentOS 7 3 内核兼容问题 原文 xff1a http www linuxfly org kubernetes 19 conflict with centos7 生产环境发现不定
  • 【转载】为什么说Prometheus是足以取代Zabbix的监控神器?

    原文 xff1a https www infoq cn article 275NDkYNZRpcTIL2R8Ms 原文 xff1a https mp weixin qq com s biz 61 MzI4NTA1MDEwNg 61 61 a
  • 【转载】K8S 问题排查:cgroup 内存泄露问题

    转载 K8S 问题排查 xff1a cgroup 内存泄露问题 原文 xff1a http www xuyasong com p 61 2049 前言 这篇文章的全称应该叫 xff1a 在某些内核版本上 xff0c cgroup 的 kme
  • 【可用】prometheus邮件报警配置

    参考 xff1a https github com prometheus alertmanager issues 384 参考 xff1a https github com easzlab kubeasz issues 448 使用QQ邮箱
  • 【案例】微服务-中台-业务关系图(来自processon)

    微服务 中台 业务关系图 知识云微服务架构 微服务开发平台规划架构图 微服务架构图 物联网微服务无业务架构 阿里中台技术在大型企业数字化转型的架构图 阿里中台战略笔记
  • prometheus监控域名证书到期时间

    参考 xff1a https mp weixin qq com s gXffcNzixAiTKSBZcf2sBA 最终效果图 xff1a 下面全部使用docker部署 xff1a 一 部署prometheus 这是一个默认的promethe
  • windows更新后无法上网问题(wifi和有线都不行)

    我真是个既倒霉又幸运的孩子 曾经 xff0c 有人在旁边抱怨 xff1a 该死的微软 xff0c 更新了一堆bug xff01 我没有理他 xff0c 因为它没有影响到我 xff0c 我很开心 曾经 xff0c 又有人在旁边抱怨 xff1a

随机推荐