-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paththreadsgroup.cpp
73 lines (60 loc) · 1.61 KB
/
threadsgroup.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
#include "stdafx.h"
#include "threadsgroup.h"
void ThreadsGroup::init(void)
{
}
void ThreadsGroup::uninit(void)
{
}
void ThreadsGroup::_createGroup(GROUP *group)
{
ZeroMemory(group, sizeof(GROUP));
}
void ThreadsGroup::_closeTerminatedHandles(GROUP *group)
{
BYTE i = 0, j = 0;
for (; i < group->count; i++)if (group->handles[i] != NULL)
{
if (WaitForSingleObject(group->handles[i], 0) == WAIT_OBJECT_0)
{
CloseHandle(group->handles[i]);
group->handles[i] = NULL;
}
else
{
group->handles[j] = group->handles[i];
j++;
}
}
group->count = j;
}
DWORD ThreadsGroup::_numberOfActiveThreads(GROUP *group)
{
DWORD count = 0;
for (BYTE i = 0; i < group->count; i++)if (group->handles[i] != NULL && WaitForSingleObject(group->handles[i], 0) == WAIT_TIMEOUT)count++;
return count;
}
bool ThreadsGroup::_createThread(GROUP *group, SIZE_T stackSize, LPTHREAD_START_ROUTINE startAddress, LPVOID parameter, LPDWORD threadId, HANDLE *threadHandle)
{
if (group->count >= MAXIMUM_WAIT_OBJECTS)
{
SetLastError(ERROR_TOO_MANY_TCBS);
return false;
}
if (startAddress)
{
HANDLE handle = CreateThread(NULL, stackSize, startAddress, parameter, 0, threadId);
if (handle == NULL)return false;
group->handles[group->count++] = handle;
if (threadHandle != NULL)*threadHandle = handle;
}
return true;
}
bool ThreadsGroup::_waitForAllExit(GROUP *group, DWORD timeout)
{
return (group->count == 0 || WaitForMultipleObjects(group->count, group->handles, TRUE, timeout) == WAIT_OBJECT_0);
}
void ThreadsGroup::_closeGroup(GROUP *group)
{
for (BYTE i = 0; i < group->count; i++)CloseHandle(group->handles[i]);
}