Linux多线程同步——信号量
线程同步
同步主线程与子线程
test.c
#include <pthread.h>
#include <sys/syscall.h>
#include <fcntl.h>
#include <semaphore.h>
#include <stdlib.h>
#include <stdio.h>void* ret_result_thread1;
sem_t *sem;void* thread1Func(void* arg)
{printf("childThread id:%u is running\n", syscall(SYS_gettid));sleep(2);sem_post(sem);pthread_exit((void*)0);}int main(int argc, char** argv)
{printf("main thread start\n");const char* semName="sem_name";sem = sem_open(semName,O_CREAT,0644,0);pthread_t thread1;int ret1;if( (ret1 = pthread_create(&thread1,NULL,thread1Func,NULL)) != 0){perror("thread1 create failed");}sem_wait(sem);printf("main thread end\n");pthread_join(thread1,ret_result_thread1);sem_close(sem);sem_unlink(semName); return 0;
}
子线程之间的同步
使用单个信号量
#include <pthread.h>
#include <sys/syscall.h>
#include <fcntl.h>
#include <semaphore.h>
#include <stdlib.h>
#include <stdio.h>void* ret_result_thread1;
void* ret_result_thread2;
sem_t *sem;void* thread1Func(void* arg)
{printf("childThread1 is running\n");sleep(2);sem_post(sem);pthread_exit((void*)0);
}void* thread2Func(void* arg)
{sem_wait(sem);printf("childThread2 is running\n"); sleep(2);pthread_exit((void*)0);
}int main(int argc, char** argv)
{printf("main thread start\n");const char* semName="sem_name";sem = sem_open(semName,O_CREAT,0644,0);pthread_t thread1,thread2;int ret1;int ret2;if( (ret1 = pthread_create(&thread1,NULL,thread1Func,NULL)) != 0){perror("thread1 create failed");}if( (ret2 = pthread_create(&thread2,NULL,thread2Func,NULL)) != 0){perror("thread2 create failed");}printf("main thread end\n");pthread_join(thread1,ret_result_thread1);pthread_join(thread2,ret_result_thread2); sem_close(sem);sem_unlink(semName); return 0;
}
使用两个信号量
test.c
#include <pthread.h>
#include <sys/syscall.h>
#include <fcntl.h>
#include <semaphore.h>
#include <stdlib.h>
#include <stdio.h>void* ret_result_thread1;
void* ret_result_thread2;
sem_t *sem1;
sem_t *sem2;void* thread1Func(void* arg)
{sem_wait(sem1);printf("childThread1 is running\n");sleep(2);sem_post(sem2);pthread_exit((void*)0);}void* thread2Func(void* arg)
{sem_wait(sem2);printf("childThread2 is running\n"); sleep(2);sem_post(sem1);pthread_exit((void*)0);
}int main(int argc, char** argv)
{printf("main thread start\n");const char* semName1="sem_name1";sem1 = sem_open(semName1,O_CREAT,0644,1);const char* semName2="sem_name2";sem2 = sem_open(semName2,O_CREAT,0644,0);pthread_t thread1,thread2;int ret1;int ret2;if( (ret1 = pthread_create(&thread1,NULL,thread1Func,NULL)) != 0){perror("thread1 create failed");}if( (ret2 = pthread_create(&thread2,NULL,thread2Func,NULL)) != 0){perror("thread2 create failed");}printf("main thread end\n");pthread_join(thread1,ret_result_thread1);pthread_join(thread2,ret_result_thread2); sem_close(sem1);sem_close(sem2);sem_unlink(semName1);sem_unlink(semName2); return 0;
}
主线程运行结束后,线程1开始运行,休眠2秒后开始运行线程2
Linux多线程同步——信号量相关推荐
- linux usleep 线程控制权_linux多线程同步—信号量
linux多线程编程-信号量 信号量机制 锁机制使用是有限制的,锁只有两种状态,即加锁和解锁,对于互斥的访问一个全局变量,这样的方式还可以对付,但是要是对于其他的临界资源,比如说多台打印机等,这种方式 ...
- linux多线程同步概览
linux多线程同步概览 临界区 互斥锁 mutex 基本函数 pthread_mutex_destroy 何时调用? 互斥锁类型? 互斥量和自旋锁的区别 条件变量 condition variabl ...
- linux多线程拷贝信号量运用于线程之间通讯遇到的printf问题
linux多线程拷贝信号量运用于线程之间通讯遇到的printf问题 1.题目内容 使用多线程实现文件的拷贝,并使用进度条进行相应的显示 2.进度 以下代码实现了多线程完成文件的拷贝,但是在使用进度条实 ...
- Linux多线程——使用信号量同步线程
http://blog.csdn.net/ljianhui/article/details/10813469/ 信号量.同步这些名词在进程间通信时就已经说过,在这里它们的意思是相同的,只不过是同步的对 ...
- Linux 多线程同步机制:互斥量、信号量、条件变量
互斥量:互斥量提供对共享资源的保护访问,它的两种状态:lock和unlock,用来保证某段时间内只有一个线程使用共享资源,互斥量的数据类型是pthread_mutex_t 主要涉及函数:pthread ...
- linux c 网络编程与信号量,详解Linux多线程使用信号量同步
信号量.同步这些名词在进程间通信时就已经说过,在这里它们的意思是相同的,只不过是同步的对象不同而已.但是下面介绍的信号量的接口是用于线程的信号量,注意不要跟用于进程间通信的信号量混淆. 一.什么是信号 ...
- Linux多线程同步的几种方式
线程的最大特点是资源的共享性,但资源共享中的同步问题是多线程编程的难点.linux下提供了多种方式来处理线程同步,最常用的是互斥锁.条件变量和信号量. 1)互斥锁(mutex) 通过锁机制实现线程间的 ...
- Linux多线程同步
1 互斥锁 互斥锁用来保证一段时间内只有一个线程在执行一段代码.必要性显而易见:假设各个线程向同一个文件顺序写入数据,最后得到的结果一定是灾难性的. 先看下面一段代码.这是一个读/写程序,它们公用一个 ...
- Linux多线程同步------条件变量
先来看下<Linux高性能服务器编程>中对条件变量的描述: 上述话可以总结为: 多线程中某一个线程依赖于另外一个线程对共享数据的改变时,就可以使用条件变量! 用消费者生产者的来理解条件变量 ...
最新文章
- python 多态_Python中的多态
- Java的字符串常量池
- 毕业后想拿大厂 Offer?你得完整拥有这些计算机知识体系!
- 17.深浅拷贝和写时拷贝
- win2003 + sqlserver2K sp4,客户端无法连接
- Photon多人游戏开发教程
- 10、java中文件的抽象表示
- 拉斯维加斯算法结合八皇后问题
- poj2186【利用强连通分量】
- python网课视频下载-python老男孩网课22期视频教程全
- js小游戏---扫雷
- FeedingBottle 3.2另一个无线破解GUI
- 呼叫中心系统okcc怎样防护盗打?
- Vue+SpringBoot打造房屋租售信息系统
- 智遥工作流——会签与多人审批区别
- 微型计算机的外储存器是指什么,微型计算机的外储存器是指
- 华为鸿蒙小插件,mate30pro鸿蒙系统无法加载时钟小部件
- es根据字段长度过滤_es根据字段长度过滤_如何进行elasticsearch查询以过滤字段的最大值?...
- 如何去掉a标签的下划线 各种样式详解
- 自己动手编译QGIS 3.18源代码,含出错大全和解决办法