extern int sem_init __P ((sem_t *__sem, int __pshared, unsigned int __value)); 函數
sem爲指向信號量結構的一個指針;pshared不爲0時此信號量在進程間共享,不然只能爲當前進程的全部線程共享;value給出了信號量的初始值。 post
函數sem_post( sem_t *sem )用來增長信號量的值。當有線程阻塞在這個信號量上時,調用這個函數會使其中的一個線程不在阻塞,選擇機制一樣是由線程的調度策略決定的。 測試
函數sem_wait( sem_t *sem )被用來阻塞當前線程直到信號量sem的值大於0,解除阻塞後將sem的值減一,代表公共資源經使用後減小。函數sem_trywait ( sem_t *sem )是函數sem_wait()的非阻塞版本,它直接將信號量sem的值減一。 spa
函數sem_destroy(sem_t *sem)用來釋放信號量sem。 .net
信號量用sem_init函數建立的,下面是它的說明:
#include<semaphore.h>
int sem_init (sem_t *sem, int pshared, unsigned int value);
這個函數的做用是對由sem指定的信號量進行初始化,設置好它的共享選項,並指定一個整數類型的初始值。pshared參數控制着信號量的類型。若是 pshared的值是0,就表示它是當前里程的局部信號量;不然,其它進程就可以共享這個信號量。咱們如今只對不讓進程共享的信號量感興趣。 (這個參數受版本影響), pshared傳遞一個非零將會使函數調用失敗。
這兩個函數控制着信號量的值,它們的定義以下所示:線程
- #include <semaphore.h>
- int sem_wait(sem_t * sem);
- int sem_post(sem_t * sem);
這兩個函數都要用一個由sem_init調用初始化的信號量對象的指針作參數。
sem_post函數的做用是給信號量的值加上一個「1」,它是一個「原子操做」---即同時對同一個信號量作加「1」操做的兩個線程是不會衝突的;而同時對同一個文件進行讀、加和寫操做的兩個程序就有可能會引發衝突。信號量的值永遠會正確地加一個「2」--由於有兩個線程試圖改變它。
sem_wait函數也是一個原子操做,它的做用是從信號量的值減去一個「1」,但它永遠會先等待該信號量爲一個非零值纔開始作減法。也就是說,若是你對一個值爲2的信號量調用sem_wait(),線程將會繼續執行,介信號量的值將減到1。若是對一個值爲0的信號量調用sem_wait(),這個函數就會地等待直到有其它線程增長了這個值使它再也不是0爲止。若是有兩個線程都在sem_wait()中等待同一個信號量變成非零值,那麼當它被第三個線程增長一個「1」時,等待線程中只有一個可以對信號量作減法並繼續執行,另外一個還將處於等待狀態。
信號量這種「只用一個函數就能原子化地測試和設置」的能力下正是它的價值所在。還有另一個信號量函數sem_trywait,它是sem_wait的非阻塞搭檔。
最後一個信號量函數是sem_destroy。這個函數的做用是在咱們用完信號量對它進行清理。下面的定義:
#include<semaphore.h>
int sem_destroy (sem_t *sem);
這個函數也使用一個信號量指針作參數,歸還本身打敗的一切資源。在清理信號量的時候若是還有線程在等待它,用戶就會收到一個錯誤。
與其它的函數同樣,這些函數在成功時都返回「0」。
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- #include <pthread.h>
- #include <semaphore.h>
- sem_t bin_sem;
- void *thread_function1(void *arg)
- {
- printf("thread_function1--------------sem_wait\n");
- sem_wait(&bin_sem);
- printf("sem_wait\n");
- while (1)
- {
- }
- }
- void *thread_function2(void *arg)
- {
- printf("thread_function2--------------sem_post\n");
- sem_post(&bin_sem);
- printf("sem_post\n");
- while (1)
- {
- }
- }
- int main()
- {
- int res;
- pthread_t a_thread;
- void *thread_result;
- res = sem_init(&bin_sem, 0, 0);
- if (res != 0)
- {
- perror("Semaphore initialization failed");
- }
- printf("sem_init\n");
- res = pthread_create(&a_thread, NULL, thread_function1, NULL);
- if (res != 0)
- {
- perror("Thread creation failure");
- }
- printf("thread_function1\n");
- sleep (5);
- printf("sleep\n");
- res = pthread_create(&a_thread, NULL, thread_function2, NULL);
- if (res != 0)
- {
- perror("Thread creation failure");
- }
- while (1)
- {
- }
- }
- sem_init
- thread_function1
- thread_function1--------------sem_wait
- sleep
- thread_function2--------------sem_post
- sem_wait
- sem_post
轉自:sem_wait sem_post信號量操做進本函數指針