信號量和互斥鎖(mutex)的區別:互斥鎖只允許一個線程進入臨界區,而信號量允許多個線程同時進入臨界區。
不多做解釋,要使用信號量同步,需要包含頭文件semaphore.h。
主要用到的函數:
下列的代碼演示了如何用信號量同步,模擬一個窗口服務系統。
/* @purpose: 基於信號量的多線程同步,操作系統原理中的P,V操作 * @author: [email protected] * @create: 2015-03-20 Fri * */ #include <pthread.h> #include <semaphore.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> /* @Scene: 某行業營業廳同時只能服務兩個顧客。 * 有多個顧客到來,每個顧客如果發現服務窗口已滿,就等待, * 如果有可用的服務窗口,就接受服務。 */ /* 將信號量定義為全局變量,方便多個線程共享 */ sem_t sem; /* 每個線程要運行的例程 */ void * get_service(void *thread_id) { /* 注意:立即保存thread_id的值,因為thread_id是對主線程中循環變量i的引用,它可能馬上被修改 */ int customer_id = *((int *)thread_id); if(sem_wait(&sem) == 0) { usleep(100); /* service time: 100ms */ printf("customer %d receive service ...\n", customer_id); sem_post(&sem); } } #define CUSTOMER_NUM 10 int main(int argc, char *argv[]) { /* 初始化信號量,初始值為2,表示有兩個顧客可以同時接收服務 */ /* @prototype: int sem_init(sem_t *sem, int pshared, unsigned int value); */ /* pshared: if pshared == 0, the semaphore is shared among threads of a process * otherwise the semaphore is shared between processes. */ sem_init(&sem, 0, 2); /* 為每個顧客定義一個線程id, pthread_t 其實是unsigned long int */ pthread_t customers[CUSTOMER_NUM]; int i, ret; /* 為每個顧客生成一個線程 */ for(i = 0; i < CUSTOMER_NUM; i++){ int customer_id = i; ret = pthread_create(&customers[i], NULL, get_service, &customer_id); if(ret != 0){ perror("pthread_create"); exit(1); } else { printf("Customer %d arrived.\n", i); } usleep(10); } /* 等待所有顧客的線程結束 */ /* 注意:這地方不能再用i做循環變量,因為可能線程中正在訪問i的值 */ int j; for(j = 0; j < CUSTOMER_NUM; j++) { pthread_join(customers[j], NULL); } /* Only a semaphore that has been initialized by sem_init(3) * should be destroyed using sem_destroy().*/ sem_destroy(&sem); return 0; }
編譯:gcc main.c -lpthread。
運行結果(注意,每次運行都不相同):
Customer 0 arrived. Customer 1 arrived. customer 0 receive service ... Customer 2 arrived. customer 1 receive service ... Customer 3 arrived. customer 2 receive service ... Customer 4 arrived. customer 3 receive service ... Customer 5 arrived. customer 4 receive service ... Customer 6 arrived. customer 5 receive service ... Customer 7 arrived. customer 6 receive service ... Customer 8 arrived. customer 7 receive service ... Customer 9 arrived. customer 8 receive service ... customer 9 receive service ...