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信号量线程同步的例子的更多相关文章

  1. linux系统编程--线程同步

    同步概念 所谓同步,即同时起步,协调一致.不同的对象,对“同步”的理解方式略有不同. 如,设备同步,是指在两个设备之间规定一个共同的时间参考: 数据库同步,是指让两个或多个数据库内容保持一致,或者按需 ...

  2. Linux系统编程 —线程同步概念

    同步概念 同步,指对在一个系统中所发生的事件之间进行协调,在时间上出现一致性与统一化的现象. 但是,对于不同行业,对于同步的理解略有不同.比如:设备同步,是指在两个设备之间规定一个共同的时间参考:数据 ...

  3. [C++] socket - 4 [线程同步 简单例子]

    /*WINAPI 线程同步*/ #include<windows.h> #include<stdio.h> DWORD WINAPI myfun1(LPVOID lpParam ...

  4. Linux多线程及线程同步简单实例

    一.多线程基本概念 1. 线程的基本概念 ① 线程就是轻量级的进程 ②线程和创建他的进程共享代码段.数据段 ③线程拥有自己的栈 2. 在实际应用中,多个线程往往会访问同一数据或资源,为避免线程之间相互 ...

  5. 【Linux】Mutex互斥量线程同步的例子

    0.互斥量  Windows下的互斥量 是个内核对象,每次WaitForSingleObject和ReleaseMutex时都会检查当前线程ID和占有互斥量的线程ID是否一致. 当多次Wait**时就 ...

  6. C#多线程---Semaphore实现线程同步

    一.简介 Semaphore类限制可同时访问某一资源或资源池的线程数.线程通过调用 WaitOne方法将信号量减1,并通过调用 Release方法把信号量加1. 构造函数:public Semapho ...

  7. Delphi多线程的OnTerminate属性(附加一个关于临界区线程同步的例子)

    首先看TThread源码中关于OnTerminate的代码: public .... property OnTerminate: TNotifyEvent read FOnTerminate writ ...

  8. linux Posix 信号量 三 (经典例子)

    本文将阐述一下信号量的作用及经典例子,当中包括“<越狱>寄信”,“家庭吃水果”,“五子棋”,“接力赛跑”,“读者写者”,“四方恋爱”等 首先,讲 semWait操作(P操作)和semSig ...

  9. linux中的线程同步:生产者、消费者问题

    #include <stdio.h> #include <semaphore.h> #include <unistd.h> #include <stdlib. ...

随机推荐

  1. 286. Walls and Gates

    题目: You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an ob ...

  2. Algorithm: cartesian tree

    http://baike.baidu.com/link?url=XUt5fXQ-jtFBM0UdKiGA41_NWFvdFSYwVsy4SVvCRRuEBvNkLfT9TgOtzsXvaOT9nuq_ ...

  3. Data Base Oracle下载及安装

    Oracle   下载及安装 一.官方下地址:   http://www.oracle.com/technetwork/database/enterprise-edition/downloads/in ...

  4. R之批处理

    在linux下如何编写脚本调用R语言写的程序呢? R语言进行批处理有2种方式: R CMD BATCH --options scriptfile outputfile Rscript --option ...

  5. 虚拟机 linux系统如何安装vmware Tools

    1.打开VMware Workstation虚拟机,开启CentOS系统 虚拟机-安装VMware Tools 登录CentOS终端命令行 2.mkdir /media/mnt    #新建挂载目录 ...

  6. sysfs实例

    转自:http://blog.chinaunix.net/u1/51562/showart_1076295.html 一:前言 在设备模型中,sysfs文件系统用来表示设备的结构.将设备的层次结构形象 ...

  7. check if a linux process is done using bash 检查进程是否在运行

    # cat > check_process_is_end.sh while truedo   sleep 30 # seconds   res=`ps -ef | grep RNAhybrid` ...

  8. vs中常用的快捷键

    VS中常用的快捷键: ctrl+s         保存 ctrl+Shift+S   保存所有VS中打开的所有文件 ctrl+O         打开新文件 ctrl+Shift+O   打开项目 ...

  9. 编程时 对 用途这个字段定义时 不要用using 这个英文

    编程时  对 用途这个字段定义时  不要用using 这个英文

  10. Machine Learning for hackers读书笔记(六)正则化:文本回归

    data<-'F:\\learning\\ML_for_Hackers\\ML_for_Hackers-master\\06-Regularization\\data\\' ranks < ...