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

1.31 实现五个线程的同步

1.使用互斥锁实现

1>程序代码
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;//创建互斥锁
pthread_mutex_t m;
pthread_mutex_t m1;
pthread_mutex_t m2;
pthread_mutex_t m3;
pthread_mutex_t m4;void* thread_main1(void* arg)
{while(1){pthread_mutex_lock(&m1);printf("1#线程\n");sleep(1);pthread_mutex_unlock(&m2);}
}void* thread_main2(void* arg)
{while(1){pthread_mutex_lock(&m2);printf("2#线程\n");sleep(1);pthread_mutex_unlock(&m3);}
}void* thread_main3(void* arg)
{while(1){pthread_mutex_lock(&m3);printf("3#线程\n");sleep(1);pthread_mutex_unlock(&m4);}
}void* thread_main4(void* arg)
{while(1){pthread_mutex_lock(&m4);printf("4#线程\n");printf("-------------\n");sleep(1);pthread_mutex_unlock(&m);}
}int main(int argc, const char *argv[])
{//初始化互斥锁pthread_mutex_init(&m,NULL);pthread_mutex_init(&m1,NULL);pthread_mutex_init(&m2,NULL);pthread_mutex_init(&m3,NULL);pthread_mutex_init(&m4,NULL);pthread_mutex_lock(&m1);pthread_mutex_lock(&m2);pthread_mutex_lock(&m3);pthread_mutex_lock(&m4);//创建4个分支线程pthread_t id1;pthread_create(&id1,0,thread_main1,0);pthread_detach(id1);pthread_t id2;pthread_create(&id2,0,thread_main2,0);pthread_detach(id2);pthread_t id3;pthread_create(&id3,0,thread_main3,0);pthread_detach(id3);pthread_t id4;pthread_create(&id4,0,thread_main4,0);pthread_detach(id4);//主线程while(1){pthread_mutex_lock(&m);printf("主线程\n");sleep(1);pthread_mutex_unlock(&m1);}//销毁互斥锁pthread_mutex_destroy(&m);pthread_mutex_destroy(&m1);pthread_mutex_destroy(&m2);pthread_mutex_destroy(&m3);pthread_mutex_destroy(&m4);return 0;
}
2>运行效果

2.使用信号量实现

1>程序代码
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;//创建信号量
sem_t s;
sem_t s1;
sem_t s2;
sem_t s3;
sem_t s4;void* thread_main1(void* arg)
{while(1){sem_wait(&s1);printf("1#线程\n");sleep(1);sem_post(&s2);}
}void* thread_main2(void* arg)
{while(1){sem_wait(&s2);printf("2#线程\n");sleep(1);sem_post(&s3);}
}void* thread_main3(void* arg)
{while(1){sem_wait(&s3);printf("3#线程\n");sleep(1);sem_post(&s4);}
}void* thread_main4(void* arg)
{while(1){sem_wait(&s4);printf("4#线程\n");printf("----------------\n");sleep(1);sem_post(&s);}
}int main(int argc, const char *argv[])
{//初始化信号量sem_init(&s,0,1);sem_init(&s1,0,1);sem_init(&s2,0,1);sem_init(&s3,0,1);sem_init(&s4,0,1);sem_wait(&s1);sem_wait(&s2);sem_wait(&s3);sem_wait(&s4);//创建4个分支线程pthread_t id1;pthread_create(&id1,0,thread_main1,0);pthread_detach(id1);pthread_t id2;pthread_create(&id2,0,thread_main2,0);pthread_detach(id2);pthread_t id3;pthread_create(&id3,0,thread_main3,0);pthread_detach(id3);pthread_t id4;pthread_create(&id4,0,thread_main4,0);pthread_detach(id4);//销毁信号量sem_destroy(&s);sem_destroy(&s1);sem_destroy(&s2);sem_destroy(&s3);sem_destroy(&s4);//主线程while(1){sem_wait(&s);printf("主线程\n");sleep(1);sem_post(&s1);}return 0;
}
2>运行效果

http://www.lryc.cn/news/529820.html

相关文章:

  • three.js+WebGL踩坑经验合集(6.1):负缩放,负定矩阵和行列式的关系(2D版本)
  • 【开源免费】基于SpringBoot+Vue.JS体育馆管理系统(JAVA毕业设计)
  • 《大数据时代“快刀”:Flink实时数据处理框架优势全解析》
  • antdesignvue统计数据源条数、计算某列合计值、小数计算不精确多了很多小数位
  • 02.05、链表求和
  • dmfldr实战
  • Kafka 副本机制(包含AR、ISR、OSR、HW 和 LEO 介绍)
  • 爬虫基础(二)Web网页的基本原理
  • 外网访问禅道软件项目管理系统
  • Python 梯度下降法(五):Adam Optimize
  • 笔试-二进制
  • springboot 2.7.6 security mysql redis jwt配置例子
  • FreeRTOS从入门到精通 第十六章(任务通知)
  • TensorFlow 简单的二分类神经网络的训练和应用流程
  • 无人机图传模块 wfb-ng openipc-fpv,4G
  • .cc扩展名是什么语言?C语言必须用.c为扩展名吗?主流编程语言扩展名?Java为什么不能用全数字的文件名?
  • 【MyDB】4-VersionManager 之 3-死锁及超时检测
  • 【Linux】使用管道实现一个简易版本的进程池
  • 【OpenGL】OpenGL游戏案例(二)
  • 28. 【.NET 8 实战--孢子记账--从单体到微服务】--简易报表--报表定时器与报表数据修正
  • Java 泛型<? extends Object>
  • FPGA|使用quartus II通过AS下载POF固件
  • “新月之智”智能战术头盔系统(CITHS)
  • php:代码中怎么搭建一个类似linux系统的crontab服务
  • 【LeetCode: 958. 二叉树的完全性检验 + bfs + 二叉树】
  • MinDoc 安装与部署
  • 从0开始使用面对对象C语言搭建一个基于OLED的图形显示框架(基础组件实现)
  • windows系统如何检查是否开启了mongodb服务
  • VS安卓仿真器下载失败怎么办?
  • 计算机网络一点事(24)