【Linux】Semaphore信号量线程同步的例子
0、 信号量
Linux下的信号量和windows下的信号量稍有不同。
Windows
Windows下的信号量有一个最大值和一个初始值,初始值和最大值可以不同。 而且Windows下的信号量是一个【内核对象】,在整个OS都可以访问到。
Linux
Linux下的信号量在创建的时候可以指定一个初始值,这个初始值也是最大值。 而且Linux下的信号量可以根据需要设置为是否是【进程间共享】的,如果不是进程间共享的则就是一个本进程局部信号量。
1、相关API
int semt_init(
semt_t* sem, //a semaphore pointer
int pshared, //0 as a local semaphore of cuurent process, or the semaphore can be shared between mulit processes
unsigned value //the init value of this memaphore
) //minus ONE value of semaphore
int sem_wait(sem_t* sem); //add ONE value of semaphore
int sem_post(sem_t* sem); //destroy the semaphore
int sem_destroy(sem_t* sem); All the functions above Rerurn ZERO IF SUCCESS !
2、上代码
这个demo创建了5个线程,信号量的初始值为2,即同时最多有2个线程可以获得获得信号量从而得到执行。
#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>
using namespace std; sem_t g_semt; void* work_thread(void* p)
{
pthread_t tID = pthread_self(); cout << "-------" << tID << " is waiting for a semaphore -------" << endl;
sem_wait(&g_semt);
cout << "-------" << tID << " got a semaphore, is Runing -------" << endl << endl;
usleep( * * ); //2 seconds
sem_post(&g_semt); static char* pRet = "thread finished! \n"; return pRet;
} int main()
{
const size_t nThreadCount = ; //amounts of thread array
const unsigned int nSemaphoreCount = ; //initial value of semaphore
int nRet = -;
void* pRet = NULL;
pthread_t threadIDs[nThreadCount] = {}; nRet = sem_init(&g_semt, , nSemaphoreCount);
if ( != nRet)
return -; for (size_t i = ; i < nThreadCount; ++ i)
{
nRet = pthread_create(&threadIDs[i], NULL, work_thread, NULL);
if ( != nRet)
continue;
} for (size_t i = ; i < nThreadCount; ++ i)
{
int nRet2 = pthread_join(threadIDs[i], &pRet);
cout << endl << threadIDs[i] << " return value is " << (char*)pRet << endl;
} cout << endl << endl; sem_destroy(&g_semt); return ;
}
4、执行情况
编译 g++ -D_REENTRANT -lpthread semaphore.cpp -g -o semaphore.out

【Linux】Semaphore信号量线程同步的例子的更多相关文章
- linux系统编程--线程同步
同步概念 所谓同步,即同时起步,协调一致.不同的对象,对“同步”的理解方式略有不同. 如,设备同步,是指在两个设备之间规定一个共同的时间参考: 数据库同步,是指让两个或多个数据库内容保持一致,或者按需 ...
- Linux系统编程 —线程同步概念
同步概念 同步,指对在一个系统中所发生的事件之间进行协调,在时间上出现一致性与统一化的现象. 但是,对于不同行业,对于同步的理解略有不同.比如:设备同步,是指在两个设备之间规定一个共同的时间参考:数据 ...
- [C++] socket - 4 [线程同步 简单例子]
/*WINAPI 线程同步*/ #include<windows.h> #include<stdio.h> DWORD WINAPI myfun1(LPVOID lpParam ...
- Linux多线程及线程同步简单实例
一.多线程基本概念 1. 线程的基本概念 ① 线程就是轻量级的进程 ②线程和创建他的进程共享代码段.数据段 ③线程拥有自己的栈 2. 在实际应用中,多个线程往往会访问同一数据或资源,为避免线程之间相互 ...
- 【Linux】Mutex互斥量线程同步的例子
0.互斥量 Windows下的互斥量 是个内核对象,每次WaitForSingleObject和ReleaseMutex时都会检查当前线程ID和占有互斥量的线程ID是否一致. 当多次Wait**时就 ...
- C#多线程---Semaphore实现线程同步
一.简介 Semaphore类限制可同时访问某一资源或资源池的线程数.线程通过调用 WaitOne方法将信号量减1,并通过调用 Release方法把信号量加1. 构造函数:public Semapho ...
- Delphi多线程的OnTerminate属性(附加一个关于临界区线程同步的例子)
首先看TThread源码中关于OnTerminate的代码: public .... property OnTerminate: TNotifyEvent read FOnTerminate writ ...
- linux Posix 信号量 三 (经典例子)
本文将阐述一下信号量的作用及经典例子,当中包括“<越狱>寄信”,“家庭吃水果”,“五子棋”,“接力赛跑”,“读者写者”,“四方恋爱”等 首先,讲 semWait操作(P操作)和semSig ...
- linux中的线程同步:生产者、消费者问题
#include <stdio.h> #include <semaphore.h> #include <unistd.h> #include <stdlib. ...
随机推荐
- C++:流类库与输入输出
7.2.1 C++的输入输出流 ios:流基类(抽象类) istream:通用输入流类和其他输入流的基类 ostream:通用输出流类和其他输出类的基类 iostream:通用输入输出流类和其他输入输 ...
- python流程控制语句 for循环 - 1
Python中for循环语句是通过遍历某一序列对象(元组.列表.字典等)来构建循环,循环结束的条件就是遍历对象完成. 语法形式: for <循环变量> in <遍历对象>: & ...
- 生产环境的redis高可用集群搭建
这里只是总结一下安装步骤 如果要了解redis集群高可用的原理,推荐仔细看一遍配置文件示例http://download.redis.io/redis-stable/redis.conf,源码包里也有 ...
- TOMCAT服务器不写端口号、不写项目名访问项目、虚拟目录配置
一.不写端口. 这个问题都被问烂了,因为TOMCAT默认的访问端口为8080,而TCP/IP协议默认80端口访问,大家之所以看到别的网站都不写端口号是因为人家用的的80端口访问的,而80端口因为的TC ...
- A - 畅通工程
A - 畅通工程 Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- 蒙特罗卡π算法(C++语言描述)
圆的面积计算公式为:S=π*r*r 将圆放到一个直角坐标系中,如图黄色部分的面积是S/4=(π*r*r)/4;如果我们将取一个单位圆,则S/4=π/4. 因为是单位圆,半径为1,所以图中红色正方形的面 ...
- mmm hardware/libhardware_legacy/power/
android源码目录下的build/envsetup.sh文件,描述编译的命令 - m: Makes from the top of the tree. - mm: Buil ...
- BZOJ 2115 Xor(抑或值最大路径)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2115 题意:给出一个带权无向图.求一条1到n的路径使得路径上权值的抑或值最大? 思路:( ...
- What is the difference between DAO and DAL?
What is the difference between DAO and DAL? The Data Access Layer (DAL) is the layer of a system tha ...
- content management system
Defination of CMS: The definition of a CMS is an application (more likely web-based), that provides ...