-
Notifications
You must be signed in to change notification settings - Fork 1
/
scheduler.cpp
228 lines (182 loc) · 4.34 KB
/
scheduler.cpp
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// {"category": "Win32", "notes": "Schedule three threads"}
#include <SDKDDKVer.h>
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <vector>
#include <memory>
#include <Windows.h>
using namespace std;
//------------------------------------------------------------------------------
//
// There are three threads in a process. The first thread prints 1 1 1 ...,
// the second one prints 2 2 2 ..., and the third one prints 3 3 3 ...
// endlessly. How do you schedule these three threads in order to print
// 1 2 3 1 2 3 ... ?
//
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//
// Implementation
//
//------------------------------------------------------------------------------
class Scheduler
{
public:
static void Main(int count, int sleep, int seconds);
};
class SimpleThread
{
public:
SimpleThread(int number, DWORD msecWait);
~SimpleThread();
HRESULT Run();
HRESULT Notify();
private:
void ThreadProc();
static DWORD WINAPI StaticThreadProc(LPVOID lpParameter);
private:
int m_number;
DWORD m_msecWait;
HANDLE m_hThread;
HANDLE m_hEvent;
bool m_running;
};
void Scheduler::Main(int count, int sleep, int seconds)
{
vector<shared_ptr<SimpleThread>> threads;
DWORD msecStart = GetTickCount();
DWORD msecRun = seconds * 1000;
DWORD msecWait = sleep * (count + 1);
for (int i = 1; i <= count; i++)
{
shared_ptr<SimpleThread> thread = shared_ptr<SimpleThread>(
new SimpleThread(i, msecWait));
if (nullptr == thread)
{
return;
}
if (FAILED(thread->Run()))
{
return;
}
threads.push_back(thread);
}
while (GetTickCount() < msecStart + msecRun)
{
for (vector<shared_ptr<SimpleThread>>::size_type i = 0;
i < threads.size();
i++)
{
Sleep(sleep);
if (FAILED(threads[i]->Notify()))
{
return;
}
}
}
}
SimpleThread::SimpleThread(int number, DWORD msecWait)
: m_number(number), m_msecWait(msecWait),
m_hThread(NULL), m_hEvent(NULL), m_running(false)
{
}
SimpleThread::~SimpleThread()
{
m_running = false;
if (m_hEvent != NULL)
{
CloseHandle(m_hEvent);
}
if (m_hThread != NULL)
{
WaitForSingleObject(m_hThread, m_msecWait);
CloseHandle(m_hThread);
}
}
HRESULT SimpleThread::Run()
{
HRESULT hr = S_OK;
if (NULL == m_hEvent)
{
m_hEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
if (NULL == m_hEvent)
{
hr = GetLastError();
}
}
if (SUCCEEDED(hr))
{
if (NULL == m_hThread)
{
m_running = true;
m_hThread = CreateThread(
nullptr,
0,
StaticThreadProc,
this,
0,
nullptr);
if (NULL == m_hThread)
{
hr = GetLastError();
m_running = false;
}
}
}
return hr;
}
HRESULT SimpleThread::Notify()
{
HRESULT hr = S_OK;
if (m_hEvent != NULL)
{
if (!SetEvent(m_hEvent))
{
hr = GetLastError();
}
}
else
{
hr = E_HANDLE;
}
return hr;
}
void SimpleThread::ThreadProc()
{
while (m_running)
{
if (WaitForSingleObject(m_hEvent, m_msecWait) == WAIT_OBJECT_0)
{
cout << m_number << " ";
}
}
}
DWORD WINAPI SimpleThread::StaticThreadProc(LPVOID lpParameter)
{
DWORD error = ERROR_SUCCESS;
SimpleThread* pThis = reinterpret_cast<SimpleThread*>(lpParameter);
if (pThis != nullptr)
{
pThis->ThreadProc();
}
else
{
error = ERROR_INVALID_PARAMETER;
}
return error;
}
//------------------------------------------------------------------------------
//
// Demo execution.
//
//------------------------------------------------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
const int threadCount = 3;
const int msecSleep = 37;
const int secondsRun = 10;
Scheduler::Main(threadCount, msecSleep, secondsRun);
cout << endl;
return 0;
}