-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOddEvenTest.cpp
More file actions
86 lines (63 loc) · 1.68 KB
/
OddEvenTest.cpp
File metadata and controls
86 lines (63 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "stdafx.h"
#include <stdio.h>
#include <Windows.h>
#include <iostream>
int arr[17];
DWORD WINAPI PrintOdd(LPVOID Param)
{
HANDLE* Mutex = (HANDLE*) Param;
for(int i = 1, count =0; i <= 5; )
{
wchar_t buffer[256];
wsprintfW(buffer, L"%d", i);
// Acquire the lock
WaitForSingleObject(*Mutex, INFINITE);
::MessageBox(NULL, buffer , L"Odd Dlg title", MB_OK);
arr[count++] = i;
ReleaseMutex(*Mutex);
i = i+ 2;
}
return 0;
}
DWORD WINAPI PrintEven(LPVOID Param)
{
HANDLE* Mutex = (HANDLE*) Param;
for(int i = 0, count = 7; i <= 6;)
{
wchar_t buffer[256];
wsprintfW(buffer, L"%d", i);
WaitForSingleObject(*Mutex, INFINITE);
::MessageBox(NULL, buffer , L"Even Dlg title", MB_OK);
arr[count++] = i;
ReleaseMutex(*Mutex);
i = i+ 2;
}
return 0;
}
void oddEvenTest()
{
DWORD Threadid;
HANDLE ThreadHandle1, ThreadHandle2;
HANDLE Mutex1;
Mutex1 = CreateMutex(NULL, FALSE, NULL);
ThreadHandle2 = CreateThread(
NULL,
0,
PrintEven, // thread function
&Mutex1,
0,
&Threadid);
ThreadHandle1 = CreateThread(
NULL,
0,
PrintOdd, // thread function
&Mutex1,
0,
&Threadid);
WaitForSingleObject(ThreadHandle1, INFINITE);
WaitForSingleObject(ThreadHandle2, INFINITE);
for(int j = 0; j < 7; j++)
{
int temp = arr[j];
}
}