-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultithr.cpp
More file actions
34 lines (31 loc) · 857 Bytes
/
Multithr.cpp
File metadata and controls
34 lines (31 loc) · 857 Bytes
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
#include "Main.h"
#include <time.h>
// exercise for multi-threading
long globalTime=0;
DWORD WINAPI timer(LPVOID Param) //全局计时器
{
while(1)
{
Sleep(1000);
WaitForSingleObject(hTimerMutex, INFINITE);
globalTime++;
ReleaseMutex(hTimerMutex);
}
}
DWORD WINAPI doAfter(LPVOID param) //倒计时执行某函数,因为只能有一个参数所以搞了struct
{
int secs=((tmr_para*)param)->secs;
WaitForSingleObject(hTimerMutex, INFINITE);
int timeNow=globalTime;
ReleaseMutex(hTimerMutex);
while(1)
{
Sleep(1000);
WaitForSingleObject(hTimerMutex, INFINITE);
if(globalTime==timeNow+secs)break;
ReleaseMutex(hTimerMutex);
}
((tmr_para*)param)->pFunction(((tmr_para*)param)->pParameter);
ReleaseMutex(hTimerMutex);
return 1;
}