信號量 例子



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(1000 * 1000 * 2);  //2 seconds
    sem_post(&g_semt);

    static char* pRet = "thread finished! \n";

    return pRet;
}

int main()
{
    const size_t nThreadCount = 5; //amounts of thread array
    const unsigned int nSemaphoreCount = 2; //initial value of semaphore
    int nRet = -1;
    void* pRet = NULL;
    pthread_t threadIDs[nThreadCount] = {0};
    
    nRet = sem_init(&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)
    {
        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 0;
}
複製代碼

 

 

 

   

 

 

4、執行情況

 編譯 g++ -D_REENTRANT  -lpthread   semaphore.cpp  -g  -o  semaphore.out

 

 


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(1000 * 1000 * 2);  //2 seconds
    sem_post(&g_semt);

    static char* pRet = "thread finished! \n";

    return pRet;
}

int main()
{
    const size_t nThreadCount = 5; //amounts of thread array
    const unsigned int nSemaphoreCount = 2; //initial value of semaphore
    int nRet = -1;
    void* pRet = NULL;
    pthread_t threadIDs[nThreadCount] = {0};
    
    nRet = sem_init(&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)
    {
        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 0;
}
複製代碼

 

 

 

   

 

 

4、執行情況

 編譯 g++ -D_REENTRANT  -lpthread   semaphore.cpp  -g  -o  semaphore.out

 

 

發佈了60 篇原創文章 · 獲贊 33 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章