1
#include
<
stdio.h
>
2 #include < Windows.h >
3
4 #define MAX_SEM_COUNT 10
5 #define THREADCOUNT 12
6
7 HANDLE ghSemaphore;
8 DWORD WINAPI ThreadProc( LPVOID );
9
10 void main( int argc, char * argv[])
11 {
12 HANDLE aThread[THREADCOUNT];
13 DWORD ThreadID;
14 int i;
15
16 // Create a semaphore with initial and max counts of MAX_SEM_COUNT
17 ghSemaphore = CreateSemaphore(
18 NULL, // default security attributes - lpSemaphoreAttributes是信号量的安全属性
19 MAX_SEM_COUNT, // initial count - lInitialCount是初始化的信号量
20 MAX_SEM_COUNT, // maximum count - lMaximumCount是允许信号量增加到最大值
21 NULL); // unnamed semaphore - lpName是信号量的名称
22
23 if (ghSemaphore == NULL)
24 {
25 printf( " CreateSemaphore error: %d\n " , GetLastError());
26 return ;
27 }
28
29 // Create worker threads
30 for ( i = 0 ; i < THREADCOUNT; i ++ )
31 {
32 aThread[i] = CreateThread(
33 NULL, // default security attributes
34 0 , // default stack size
35 (LPTHREAD_START_ROUTINE) ThreadProc,
36 NULL, // no thread function arguments
37 0 , // default creation flags
38 & ThreadID); // receive thread identifier
39
40 if ( aThread[i] == NULL )
41 {
42 printf( " CreateThread error: %d\n " , GetLastError());
43 return ;
44 }
45 }
46
47 // Wait for all threads to terminate
48 WaitForMultipleObjects(THREADCOUNT, aThread, TRUE, INFINITE);
49
50 // Close thread and semaphore handles
51 for ( i = 0 ; i < THREADCOUNT; i ++ )
52 CloseHandle(aThread[i]);
53
54 CloseHandle(ghSemaphore);
55 }
56
57 DWORD WINAPI ThreadProc( LPVOID lpParam )
58 {
59 DWORD dwWaitResult;
60 BOOL bContinue = TRUE;
61
62 while (bContinue)
63 {
64 // Try to enter the semaphore gate.
65 dwWaitResult = WaitForSingleObject(
66 ghSemaphore, // handle to semaphore
67 0L ); // zero-second time-out interval
68
69 switch (dwWaitResult)
70 {
71 // The semaphore object was signaled.
72 case WAIT_OBJECT_0:
73 // TODO: Perform task
74 printf( " Thread %d: wait succeeded\n " , GetCurrentThreadId());
75
76 // Ensure a thread performs only once
77 bContinue = FALSE;
78
79 // Simulate thread spending time on task
80 Sleep( 5 );
81
82 // Release the semaphore when task is finished
83
84 if ( ! ReleaseSemaphore(
85 ghSemaphore, // handle to semaphore - hSemaphore是要增加的信号量句柄
86 1 , // increase count by one - lReleaseCount是增加的计数
87 NULL) ) // not interested in previous count - lpPreviousCount是增加前的数值返回
88 {
89 printf( " ReleaseSemaphore error: %d\n " , GetLastError());
90 }
91 break ;
92
93 // The semaphore was nonsignaled, so a time-out occurred.
94 case WAIT_TIMEOUT:
95 printf( " Thread %d: wait timed out\n " , GetCurrentThreadId());
96 break ;
97 }
98 }
99 return TRUE;
100 }
2 #include < Windows.h >
3
4 #define MAX_SEM_COUNT 10
5 #define THREADCOUNT 12
6
7 HANDLE ghSemaphore;
8 DWORD WINAPI ThreadProc( LPVOID );
9
10 void main( int argc, char * argv[])
11 {
12 HANDLE aThread[THREADCOUNT];
13 DWORD ThreadID;
14 int i;
15
16 // Create a semaphore with initial and max counts of MAX_SEM_COUNT
17 ghSemaphore = CreateSemaphore(
18 NULL, // default security attributes - lpSemaphoreAttributes是信号量的安全属性
19 MAX_SEM_COUNT, // initial count - lInitialCount是初始化的信号量
20 MAX_SEM_COUNT, // maximum count - lMaximumCount是允许信号量增加到最大值
21 NULL); // unnamed semaphore - lpName是信号量的名称
22
23 if (ghSemaphore == NULL)
24 {
25 printf( " CreateSemaphore error: %d\n " , GetLastError());
26 return ;
27 }
28
29 // Create worker threads
30 for ( i = 0 ; i < THREADCOUNT; i ++ )
31 {
32 aThread[i] = CreateThread(
33 NULL, // default security attributes
34 0 , // default stack size
35 (LPTHREAD_START_ROUTINE) ThreadProc,
36 NULL, // no thread function arguments
37 0 , // default creation flags
38 & ThreadID); // receive thread identifier
39
40 if ( aThread[i] == NULL )
41 {
42 printf( " CreateThread error: %d\n " , GetLastError());
43 return ;
44 }
45 }
46
47 // Wait for all threads to terminate
48 WaitForMultipleObjects(THREADCOUNT, aThread, TRUE, INFINITE);
49
50 // Close thread and semaphore handles
51 for ( i = 0 ; i < THREADCOUNT; i ++ )
52 CloseHandle(aThread[i]);
53
54 CloseHandle(ghSemaphore);
55 }
56
57 DWORD WINAPI ThreadProc( LPVOID lpParam )
58 {
59 DWORD dwWaitResult;
60 BOOL bContinue = TRUE;
61
62 while (bContinue)
63 {
64 // Try to enter the semaphore gate.
65 dwWaitResult = WaitForSingleObject(
66 ghSemaphore, // handle to semaphore
67 0L ); // zero-second time-out interval
68
69 switch (dwWaitResult)
70 {
71 // The semaphore object was signaled.
72 case WAIT_OBJECT_0:
73 // TODO: Perform task
74 printf( " Thread %d: wait succeeded\n " , GetCurrentThreadId());
75
76 // Ensure a thread performs only once
77 bContinue = FALSE;
78
79 // Simulate thread spending time on task
80 Sleep( 5 );
81
82 // Release the semaphore when task is finished
83
84 if ( ! ReleaseSemaphore(
85 ghSemaphore, // handle to semaphore - hSemaphore是要增加的信号量句柄
86 1 , // increase count by one - lReleaseCount是增加的计数
87 NULL) ) // not interested in previous count - lpPreviousCount是增加前的数值返回
88 {
89 printf( " ReleaseSemaphore error: %d\n " , GetLastError());
90 }
91 break ;
92
93 // The semaphore was nonsignaled, so a time-out occurred.
94 case WAIT_TIMEOUT:
95 printf( " Thread %d: wait timed out\n " , GetCurrentThreadId());
96 break ;
97 }
98 }
99 return TRUE;
100 }