Windows core programming does not take much space to introduce, as long as you understand its counting principle can easily grasp.
Semaphore rules:
- If the current resource count is greater than 0, the semaphore is triggered.
- If the current resource count is equal to 0, the semaphore is not triggered.
- The current resource count is no less than 0.
- The current resource count does not exceed the maximum resource count.
Create a function prototype for semaphores:
HANDLE WINAPI CreateSemaphore( __in_opt LPSECURITY_ATTRIBUTES lpSemaphoreAttributes, __in LONG lInitialCount, __in LONG lMaximumCount, __in_opt LPCTSTR lpName);
If the first parameter is null, the default security attribute is used. The second parameter indicates the number of semaphores. The third parameter indicates the maximum number of resources of the semaphore. You can pass a string pointer to the fourth parameter to specify the semaphore name, so that you can use the FunctionOpensemaphoreTo open the semaphore with the specified name.
The function prototype used to increase the resource count of semaphores is:
BOOL WINAPI ReleaseSemaphore( __in HANDLE hSemaphore, __in LONG lReleaseCount, __out_opt LPLONG lpPreviousCount);
The second parameter indicates the number of added resources. The third parameter returns the current resource count.
I want to create a semaphore with a maximum resource Number of 5 and the current resource Number of 4, which can be like this:
Hsemaphore = createsemaphore (null, 4,5, null );
In this way, if there are four waiting threads, they may obtain the semaphores to run at the same time. The wait function detects the current resource count. If it is greater than 0, it obtains the semaphore and reduces the resource count of the semaphore by one. The detection, increase, and decrease of the Resource Count of semaphores are performed in an atomic manner and will not be disturbed by other threads. When the Resource Count processing is complete, other threads can process the resource count. The speed of atomic operations is very fast, which may lead to the illusion of parallel threads.
Use an example program to learn.
# Include <windows. h >#include <iostream >#include <ctime> # include <fstream> using namespace STD; handle hsemaphore; struct threadinfo {int IID; long icount; threadinfo * pthreadinfo; int isleep;}; DWORD winapi threadproc (lpvoid LP) {threadinfo * pthreadinfo = (threadinfo *) LP; waitforsingleobject (hsemaphore, infinite); sleep (pthreadinfo-> isleep ); cout <"Thread" <pthreadinfo-> IID <": Sleep" <pthreadinfo-> isleep <Endl; releasesemaphore (hsemaphore, 1, & (pthreadinfo-> icount); cout <"Thread" <pthreadinfo-> IID <": Number of current semaphore resources: "<pthreadinfo-> icount <" semaphore Resource Count Plus 1 "<Endl; Delete pthreadinfo-> pthreadinfo; return 0 ;}int main (INT argc, char * argv []) {cout <"current number of semaphores: 4, Max number of resources: 5" <Endl; hsemaphore = createsemaphore (null, null ); handle hthread [8]; srand (unsigned) time (0); For (INT I = 0; I <8; ++ I) {threadinfo * threadinfo = new threadinfo; threadinfo-> pthreadinfo = threadinfo; threadinfo-> IID = I; threadinfo-> icount =-1; threadinfo-> isleep = rand () % 1000; hthread [I] = createthread (null, 0, threadproc, (lpvoid) threadinfo, 0, null);} waitformultipleobjects (8, hthread, true, infinite ); for (INT I = 0; I <8; ++ I) closehandle (hthread [I]); closehandle (hsemaphore); Return 0 ;}
To demonstrate the parallel effect, a random sleep time is obtained by the thread after the semaphore is obtained. The number of semaphores created is 4. Therefore, in principle, four threads may obtain semaphores at the same time. Internally, after processing the resource count in an atomic manner, the system goes to sleep. Check a running task.
The atomic operation speed is very fast. Thread 3 and Thread 0 have a very close sleep time and obtain semaphores. Therefore, when running the output at the same time, it will be written to the same row. The first four threads all "Parallel" to obtain the semaphore. When the fifth thread obtains the semaphore, it may be a thread with the least sleep (thread 4) or the thread that first obtains the semaphore (thread 1) after the running is completed, the semaphore count is added to thread 1 and thread 5 to get the chance to run the semaphore. The four threads that first obtain the semaphores have run successively. The current semaphores output by the subsequent threads can be seen.
This is only the result of a certain running, and it is hard to say that the thread is running, depending on the CPU "mood.
Hope to help those who are as confused as me.