Example of "Linux" semaphore semaphore thread synchronization

Source: Internet
Author: User
Tags semaphore

0. Signal Volume

The semaphore under Linux is slightly different from the amount of semaphores under Windows.

Windows

The semaphore under Windows has a maximum value and an initial value, and the initial and maximum values can be different. And the semaphore under Windows is a "kernel object" that can be accessed throughout the OS.

Linux

The semaphore under Linux can specify an initial value when it is created, and the initial value is the maximum value. And the amount of signal under Linux can be set to whether it is "inter-process sharing", if it is not shared between the process is a local signal volume of the process.

1. Related API

intSemt_init (semt_t* SEM,//a semaphore pointer intPshared,//0 as a local semaphore of cuurent process, or the semaphore can be shared between mulit processesUnsigned value//The init value of this Memaphore )//minus one value of semaphoreintSem_wait (sem_t*sem);//add one value of semaphoreintSem_post (sem_t*sem);//destroy the semaphoreintSem_destroy (sem_t*SEM); All the functions above Rerurn ZERO IF SUCCESS !

2, on the code

This demo creates 5 threads, the initial value of the semaphore is 2, which means that at the same time, up to 2 threads can get the semaphore to be executed.

#include <iostream>#include<pthread.h>#include<unistd.h>#include<semaphore.h>using namespacestd;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);//2 secondsSem_post (&g_semt); Static Char* PRet ="thread finished! \ n"; returnPRet;}intMain () {Constsize_t Nthreadcount =5;//amounts of thread array    ConstUnsignedintNsemaphorecount =2;//initial value of Semaphore    intNret =-1; void* PRet =NULL; pthread_t Threadids[nthreadcount]= {0}; Nret= Sem_init (&AMP;G_SEMT,0, Nsemaphorecount); if(0!=nret)return-1;  for(size_t i =0; i < Nthreadcount; ++i) {nret= Pthread_create (&Threadids[i], NULL, work_thread, NULL); if(0!=nret)Continue; }     for(size_t i =0; i < Nthreadcount; ++i) {intNRet2 = Pthread_join (Threadids[i], &PRet); cout<< Endl << Threadids[i] <<"return value is"<< (Char*) PRet <<Endl; } cout<< Endl <<Endl; Sem_destroy (&g_semt); return 0;}

4. Status of implementation

Compiling g++-d_reentrant-lpthread semaphore.cpp-g-o semaphore.out

Example of "Linux" semaphore semaphore thread synchronization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.