|
|
1.1 root 1: Sample: Using Semaphores to Control Threads
2:
3: Summary:
4:
5: The SEMA sample application shows how to control four
6: threads with a semaphore. It demonstrates this by having
7: four threads competing for the right to draw their color to
8: a rectangle in the center of the window. Access to the
9: center area is controlled by the semaphore.
10:
11: More Information:
12:
13: When you start the application you will see five rectangles:
14: a dynamic rectangle in the center (always changing color),
15: and four static rectangles surrounding it. Each of the four
16: rectangles has its own color: red, blue, green, and gray.
17: The one in the center alternates between these colors.
18:
19: The four static rectangles represent four threads. These
20: four threads compete for the rectangle in the middle, and
21: their access is controlled by the semaphore. When a thread
22: gains control of the semaphore, it gets to draw its color in
23: the center rectangle. (Note: The threads do not actually
24: draw any of the four static rectangles. To make the code
25: simpler, this is handled in the WM_PAINT message in the
26: MainWndProc function. The rectangles are used only as visual
27: representations of the threads. The threads do, however,
28: draw the rectangle in the center with their specific color.)
29:
30: The semaphore has a use count. When it is set to zero, any
31: thread can access the semaphore and execute the code within
32: its "semaphore gate" by using WaitForSingleObject. When the
33: thread gains control of the semaphore using this call, the
34: use count is incremented by 1. When the thread is done
35: executing its code, it can call ReleaseSemaphore. This will
36: decrement the count by whatever value you indicate (this
37: code uses 1), signaling to any other thread that it may gain
38: control of the semaphore.
39:
40: Note: Any thread that has access to the semaphore may
41: decrement the semaphore's use count with ReleaseSemaphore;
42: the thread does not have to have control of the semaphore at
43: the time.
44:
45: In this code, the WM_CREATE message in MainWndProc creates a
46: semaphore. The four threads are then created, each waiting
47: on the semaphore.
48:
49: Each of the threads loop, blocking on a WaitForSingleObject
50: call. Once any thread has set the use count to 0, the thread
51: gets to draw the center rectangle with its color and then
52: sleeps for half a second before freeing the semaphore again.
53: The thread then runs through the loop again.
54:
55: The followin is a list of the Win32 API used in this sample:
56:
57: BeginPaint GetClientRect PostQuitMessa TerminateThrea
58: ge d
59: CreateSemapho GetDC Rectangle WaitForSingleO
60: re bject
61: CreateSolidBr GetLastError RegisterClass wsprintf
62: ush
63: CreateThread GetMessage ReleaseDC
64: CreateWindow GetStockObjec ReleaseSemaph
65: t ore
66: DeleteObject LoadCursor SelectObject
67: DispatchMessa LoadIcon ShowWindow
68: ge
69: EndPaint MessageBox Sleep
70:
71:
72:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.