基于信号量(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的控制来对不一样进程的执行分别进行控制的方法。进程