-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadfun_queue.c
More file actions
78 lines (63 loc) · 1.87 KB
/
Copy paththreadfun_queue.c
File metadata and controls
78 lines (63 loc) · 1.87 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
#include <windows.h>
#include <stdio.h>
#define WORKER_COUNT 4
#define JOB_COUNT 20
/*
* Dynamic dispatch via shared queue.
* Contrast with threadfun.c, which uses a static [start,end) partition.
*
* Here all threads share a single nextJob index. The CRITICAL_SECTION
* protects the read-increment-release of that index so no job is claimed
* by two threads and no job is skipped.
*/
typedef struct SharedQueue {
int* jobs;
int jobCount;
int nextJob;
CRITICAL_SECTION cs;
HANDLE cancelEvent;
} SharedQueue;
DWORD WINAPI WorkerThread(LPVOID lpParam) {
SharedQueue* q = (SharedQueue*)lpParam;
while (1) {
if (WaitForSingleObject(q->cancelEvent, 0) == WAIT_OBJECT_0) {
return 0;
}
EnterCriticalSection(&q->cs);
int job = -1;
if (q->nextJob < q->jobCount) {
job = q->jobs[q->nextJob++];
}
LeaveCriticalSection(&q->cs);
if (job < 0) {
return 0;
}
printf("Thread %lu processing %d\n", GetCurrentThreadId(), job);
Sleep(200);
}
}
int main() {
int jobs[JOB_COUNT];
HANDLE workers[WORKER_COUNT];
SharedQueue q;
for (int i = 0; i < JOB_COUNT; i++) {
jobs[i] = i;
}
InitializeCriticalSection(&q.cs);
q.cancelEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
q.jobs = jobs;
q.jobCount = JOB_COUNT;
q.nextJob = 0;
for (int i = 0; i < WORKER_COUNT; i++) {
// All threads receive the same pointer — synchronization is inside the queue.
workers[i] = CreateThread(NULL, 0, WorkerThread, &q, 0, NULL);
}
// Early-stop path: SetEvent(q.cancelEvent);
WaitForMultipleObjects(WORKER_COUNT, workers, TRUE, INFINITE);
for (int i = 0; i < WORKER_COUNT; i++) {
CloseHandle(workers[i]);
}
CloseHandle(q.cancelEvent);
DeleteCriticalSection(&q.cs);
return 0;
}