1 |
joko |
1.1 |
#include <stdio.h> |
2 |
|
|
#include <windows.h> |
3 |
|
|
|
4 |
|
|
#include "bakery.h" |
5 |
|
|
|
6 |
|
|
static MY_LPCRITICAL_SECTION lpsec; |
7 |
|
|
static int value; |
8 |
|
|
|
9 |
|
|
DWORD WINAPI thread_func(LPVOID arg) { |
10 |
|
|
int myId = (int) arg; |
11 |
|
|
int iter = 0; |
12 |
|
|
|
13 |
|
|
do { |
14 |
|
|
|
15 |
|
|
My_EnterCriticalSection( myId, lpsec ); |
16 |
|
|
|
17 |
|
|
/*----*/ |
18 |
|
|
value = myId; |
19 |
|
|
Sleep(10); |
20 |
|
|
if (value != myId) { |
21 |
|
|
printf("Thread %d: invalid state! (iter: %d)\n",myId,iter); |
22 |
|
|
} else { |
23 |
|
|
printf("Thread %d: ok! (iter: %d)\n",myId,iter); |
24 |
|
|
} |
25 |
|
|
/*----*/ |
26 |
|
|
|
27 |
|
|
My_LeaveCriticalSection( myId, lpsec ); |
28 |
|
|
|
29 |
|
|
iter++; |
30 |
|
|
} while (iter < 30); |
31 |
|
|
return 0; |
32 |
|
|
} |
33 |
|
|
|
34 |
|
|
|
35 |
|
|
main () { |
36 |
|
|
HANDLE th1, th2, th3; |
37 |
|
|
My_InitializeCriticalSection( &lpsec ); |
38 |
|
|
|
39 |
|
|
th1 = CreateThread( NULL, 0, thread_func, (void*) 1, 0, NULL ); |
40 |
|
|
th2 = CreateThread( NULL, 0, thread_func, (void*) 2, 0, NULL ); |
41 |
|
|
th3 = CreateThread( NULL, 0, thread_func, (void*) 3, 0, NULL ); |
42 |
|
|
|
43 |
|
|
WaitForSingleObject( th1, INFINITE ); |
44 |
|
|
WaitForSingleObject( th2, INFINITE ); |
45 |
|
|
WaitForSingleObject( th3, INFINITE ); |
46 |
|
|
|
47 |
|
|
My_DeleteCriticalSection( lpsec ); |
48 |
|
|
} |
49 |
|
|
|