首页 > 代码库 > Semaphore用法

Semaphore用法

HANDLE hSemaphore;
cout<<1<<endl;

hSemaphore = CreateSemaphore( NULL, 0, 10000, NULL);  //初始 0个资源

ReleaseSemaphore(hSemaphore, 1, NULL); //+1
ReleaseSemaphore(hSemaphore, 1, NULL); //+1

DWORD dwWaitResult = WaitForSingleObject(hSemaphore, INFINITE);  //等待到 -1
cout<<2<<endl;
WaitForSingleObject(hSemaphore, INFINITE); //等待到 -1
cout<<3<<endl;

WaitForSingleObject(hSemaphore, INFINITE); //此时资源为0,阻塞在这里等待资源增加才能继续
cout<<4<<endl;

Semaphore用法