基於信號量(Semaphore Value)的同步,信號量能夠實現多個同類資源的多線程互斥和同步。當信號量爲單值信號量是,也能夠完成一個資源的互斥訪問。信號量(Semaphore),有時被稱爲信號燈,是在多線程環境下使用的一種設施, 它負責協調各個線程, 以保證它們可以正確、合理的使用公共資源。多線程
HANDLE WINAPI CreateSemaphore( _In_opt_ LPSECURITY_ATTRIBUTES lpSemaphoreAttributes, _In_ LONG lInitialCount, _In_ LONG lMaximumCount, _In_opt_ LPCTSTR lpName );
BOOL WINAPI ReleaseSemaphore( _In_ HANDLE hSemaphore, _In_ LONG lReleaseCount, _Out_opt_ LPLONG lpPreviousCount );
BOOL WINAPI CloseHandle( _In_ HANDLE hObject );
經過信號量同步能夠實現多進程的邏輯執行控制,CreateSemaphore的關鍵是第二個參數lInitialCount的值,若是是0系統默認是non-signaled狀態,若是lInitialCount=lMaximumCount則默認狀態爲signaled狀態,系統是能夠經過WaitForSingleObject的,所以多進程邏輯判斷是能夠先創建一個默認值爲lMaximumCount的,其餘均爲0的。線程
#include "stdafx.h" #include <Windows.h> #include <process.h> int num = 0; CRITICAL_SECTION cs; HANDLE hMutex[3] = { NULL, NULL, NULL }; unsigned WINAPI ThreadInc(void *arg) { int cnt = *(int*)arg; for (int i = 0; i < cnt; i++) { WaitForSingleObject(hMutex[0], INFINITE); num += 3; printf("Inc "); ReleaseSemaphore(hMutex[1], 1, NULL); Sleep(10); } return 0; } unsigned WINAPI ThreadDec1(void *arg) { int cnt = *(int*)arg; for (int i = 0; i < cnt; i++) { WaitForSingleObject(hMutex[1], INFINITE); num -= 1; printf("Dec1 "); ReleaseSemaphore(hMutex[2], 1, NULL); Sleep(10); } return 0; } unsigned WINAPI ThreadDec2(void *arg) { int cnt = *(int*)arg; for (int i = 0; i < cnt; i++) { WaitForSingleObject(hMutex[2], INFINITE); num -= 2; printf("Dec2 "); ReleaseSemaphore(hMutex[0], 1, NULL); Sleep(10); } return 0; } int main() { int param = 50; HANDLE h[3]; hMutex[0] = CreateSemaphore(NULL, 1, 1, NULL); hMutex[1] = CreateSemaphore(NULL, 0, 1, NULL); hMutex[2] = CreateSemaphore(NULL, 0, 1, NULL); h[0]= (HANDLE)_beginthreadex(NULL, 0, ThreadInc, ¶m, 0, NULL); if (h[0] == 0) { printf("Can not create a thread 1.\n"); return 0; } h[1] = (HANDLE)_beginthreadex(NULL, 0, ThreadDec1, ¶m, 0, NULL); if (h[1] == 0) { printf("Can not create a thread 2.\n"); return 0; } h[2] = (HANDLE)_beginthreadex(NULL, 0, ThreadDec2, ¶m, 0, NULL); if (h[1] == 0) { printf("Can not create a thread 3.\n"); return 0; } WaitForMultipleObjects(3, h, true, INFINITE); CloseHandle(hMutex[0]); CloseHandle(hMutex[1]); CloseHandle(hMutex[2]); printf("The num is %d, and end of main.\n", num); return 0; }
在主進程中創建了兩個信號量,一個是signaled狀態的,一個是non-signaled狀態的,執行時第一個進程順利經過WaitForSingleObject,執行後調用ReleaseSemaphore來釋放第二個信號量,這時第二個進程中的WaitForSingleObject了,如此往復執行就能夠完成這種邏輯順序控制了。code
Inc Dec1 Dec2 Inc Dec1 Dec2 Inc Dec1 Dec2 Inc Dec1 Dec2 Inc Dec1 Dec2 Inc Dec1 D ec2 Inc Dec1 Dec2 Inc Dec1 Dec2 Inc Dec1 Dec2 Inc Dec1 Dec2 Inc Dec1 Dec2 Inc De c1 Dec2 Inc Dec1 Dec2 Inc Dec1 Dec2 Inc Dec1 Dec2 Inc Dec1 Dec2 Inc Dec1 Dec2 In c Dec1 Dec2 Inc Dec1 Dec2 Inc Dec1 Dec2 Inc Dec1 Dec2 Inc Dec1 Dec2 Inc Dec1 Dec 2 Inc Dec1 Dec2 Inc Dec1 Dec2 Inc Dec1 Dec2 Inc Dec1 Dec2 Inc Dec1 Dec2 Inc Dec1 Dec2 Inc Dec1 Dec2 Inc Dec1 Dec2 Inc Dec1 Dec2 Inc Dec1 Dec2 Inc Dec1 Dec2 Inc Dec1 Dec2 Inc Dec1 Dec2 Inc Dec1 Dec2 Inc Dec1 Dec2 Inc Dec1 Dec2 Inc Dec1 Dec2 Inc Dec1 Dec2 Inc Dec1 Dec2 Inc Dec1 Dec2 Inc Dec1 Dec2 Inc Dec1 Dec2 Inc Dec1 D ec2 Inc Dec1 Dec2 Inc Dec1 Dec2 Inc Dec1 Dec2 Inc Dec1 Dec2 The num is 0, and en d of main.
查看運行結果就能夠看到執行順序是按照 Inc-Dec1-Dec2 的順序執行的,所以,能夠看出經過對Semaphore的控制來對不一樣進程的執行分別進行控制的方法。進程