瞭解線程信號量的基礎知識,對深刻理解python的線程會大有幫助。python
當兩個線程同時執行時,不可避免同時操做同一個變量或者文件等,因此須要有一組機制來確保他們能正確的運行:信號量和互斥量。信號量能夠分爲最簡單的「二進制信號量」和更通用的「計數信號量」。信號量一般用來保護一段代碼,使其每次只能被一個執行線程運行,這種狀況下須要用到二進制信號量。有時候但願能夠容許有限數目的線程執行一段指定代碼,這就須要用到計數信號量。實際上,技術信號量是一種二進制信號量的邏輯擴展,實際二者調用的函數同樣。函數
互斥量和信號量很類似,事實上他們能夠互相經過對方來實現。但在實際應用中,對於一些狀況使用其中一種更符合語義並且效果更好。post
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <string.h> #include <pthread.h> #include <semaphore.h> void *thread_function(void *arg); sem_t bin_sem; #define WORK_SIZE 1024 char work_area[WORK_SIZE]; /* 用來存放輸入內容 */ int main() { int res; /* 暫存一些命令的返回結果 */ pthread_t a_thread; /* 織帶新建的線程 */ void *thread_result; /* 存放線程處理結果 */ res = sem_init(&bin_sem, 0, 0); /* 初始化信號量,而且設置初始值爲0*/ if (res != 0) { perror("Semaphore initialization failed"); exit(EXIT_FAILURE); } res = pthread_create(&a_thread, NULL, thread_function, NULL); /* 建立新線程 */ if (res != 0) { perror("Thread creation failed"); exit(EXIT_FAILURE); } printf("Inout some text, Enter 'end' to finish\n"); while(strncmp("end", work_area, 3) != 0) { /* 當工做區內不是以end開頭的字符串時...*/ fgets(work_area, WORK_SIZE, stdin); /* 從標準輸入獲取輸入到worl_area */ sem_post(&bin_sem); /* 信號量+1 */ } printf("\nWaiting for thread to finish...\n"); res = pthread_join(a_thread, &thread_result); /* 等待線程結束 */ if (res != 0) { perror("Thread join failed"); exit(EXIT_FAILURE); } printf("Thread joined\n"); sem_destroy(&bin_sem); /* 銷燬信號量 */ exit(EXIT_SUCCESS); } void *thread_function(void *arg) { sem_wait(&bin_sem); /* 等待信號量有大於0的值而後-1 */ while(strncmp("end", work_area, 3) != 0) { printf("You input %ld characters\n", strlen(work_area)-1); /* 獲取輸入字符串長度 8*/ sem_wait(&bin_sem); /* 等待信號量有大於0的值而後-1 */ } pthread_exit(NULL); }
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <string.h> #include <pthread.h> #include <semaphore.h> void *thread_function(void *arg); pthread_mutex_t work_mutex; #define WORK_SIZE 1024 char work_area[WORK_SIZE]; int time_to_exit = 0; /* 用來控制是否退出*/ int main() { int res; pthread_t a_thread; void *thread_result; res = pthread_mutex_init(&work_mutex,NULL); /* 初始化一個互斥鎖 */ if (res != 0) { perror("Mutex initialization failed"); exit(EXIT_FAILURE); } res = pthread_create(&a_thread, NULL, thread_function, NULL); /* 建立一個新線程 */ if (res != 0) { perror("Thread creation failed"); exit(EXIT_FAILURE); } pthread_mutex_lock(&work_mutex); /* 嘗試對互斥量加鎖 */ printf("Input some text, Enter 'end' to finish\n"); while(!time_to_exit) { /* 檢查是否是該退出*/ fgets(work_area, WORK_SIZE, stdin); /* 從標準輸入獲取輸入到work_area */ pthread_mutex_unlock(&work_mutex); /* 解鎖互斥量 */ while(1) { pthread_mutex_lock(&work_mutex); if (work_area[0] != '\0') { /* 持續檢查work_area 是否爲空, 若是不爲空繼續等待,若是爲空,則從新讀取輸入到work_area*/ pthread_mutex_unlock(&work_mutex); sleep(1); } else { break; } } } pthread_mutex_unlock(&work_mutex); printf("\nWaiting for thread to finish...\n"); res = pthread_join(a_thread, &thread_result); if (res != 0) { perror("Thread join failed"); exit(EXIT_FAILURE); } printf("Thread joined\n"); pthread_mutex_destroy(&work_mutex); exit(EXIT_SUCCESS); } void *thread_function(void *arg) { sleep(1); pthread_mutex_lock(&work_mutex); /* 嘗試加鎖互斥量 */ while(strncmp("end", work_area, 3) != 0) { /* 當work_area裏的值不是以end開頭時*/ printf("You input %ld characters\n", strlen(work_area) -1); /* 輸出輸入的字符長度 */ work_area[0] = '\0'; /* work_area設置爲空 */ pthread_mutex_unlock(&work_mutex); sleep(1); pthread_mutex_lock(&work_mutex); while (work_area[0] == '\0') { /* 持續檢查work_area 直到它裏面有輸入值*/ pthread_mutex_unlock(&work_mutex); sleep(1); pthread_mutex_lock(&work_mutex); } } time_to_exit = 1; /* 當輸入end後,設置退出標誌 */ work_area[0] = '\0'; pthread_mutex_unlock(&work_mutex); pthread_exit(0); }