-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreadpoolWorkQueue.nim
167 lines (153 loc) · 5.49 KB
/
threadpoolWorkQueue.nim
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
## Threaded pool work queue
## nim c -r --d:threads
##
## Type [c][Enter] => create a thread worker
## Type [a][Enter] => add work to the pool
## Type [q][Enter] => quit
##
## Instead of starting workers manually (done for interactive illustrative purposes) the following could be done
##
## ```
## import cpuinfo
## for i in 0 .. countProcessors():
## spawn worker()
## ```
import os, locks, strutils, threadpool, strformat
var
aQueue : seq[int]
pQueue : ptr seq[int]
queueLock : Lock
sleepTime = 100 # Time for thread to sleep in ms so we don't peg the CPU while waiting. Defaulted to 100ms (1/10 second).
initLock(queueLock)
pQueue = addr aQueue
proc worker() =
let thrID = getThreadID()
echo &"Thread# {thrID:5}: Started."
while true:
sleep sleepTime
var
workItem : int
workToDo : bool
withLock queueLock:
if pQueue[].len > 0:
echo &"Thread# {thrID:5}: Work found. Taking one off the stack."
workItem = pQueue[].pop
workToDo = true
else:
discard
# echo thrID, ": waiting for work."
if workToDo:
echo &"Thread# {thrID:5}: Processing work item with value => {workItem:2}"
workToDo = false
# Do something here
echo """
[c][Enter] => create a thread worker
[a][Enter] => add work to the pool
[q][Enter] => quit
"""
while true:
let a = readLine(stdin)
case a.normalize
of "c":
spawn worker()
of "a":
withLock queueLock:
pQueue[].add @[0,1,2,3,4,5,6,7,8,9,10]
of "q":
echo "Exiting."
quit()
# import cpuinfo
# for i in 0 .. countProcessors():
# spawn worker()
# withLock queueLock:
# for i in 0 .. 100:
# pQueue[].add @[0,1,2,3,4,5,6,7,8,9,10]
sync()
#[
EXAMPLE OUTPUT:
[c][Enter] => create a thread worker
[a][Enter] => add work to the pool
[q][Enter] => quit
a
a
a
a
c
Thread# 14484: Started.
c
Thread# 5232: Started.
Thread# 14484: Work found. Taking one off the stack.
Thread# 14484: Processing work item with value => 7
c
Thread# 14944: Started.
Thread# 5232: Work found. Taking one off the stack.
Thread# 5232: Processing work item with value => 6
Thread# 14484: Work found. Taking one off the stack.
Thread# 14484: Processing work item with value => 5
Thread# 14944: Work found. Taking one off the stack.
Thread# 14944: Processing work item with value => 4
Thread# 5232: Work found. Taking one off the stack.
Thread# 5232: Processing work item with value => 3
Thread# 14484: Work found. Taking one off the stack.
Thread# 14484: Processing work item with value => 2
Thread# 14944: Work found. Taking one off the stack.
Thread# 14944: Processing work item with value => 1
Thread# 5232: Work found. Taking one off the stack.
Thread# 5232: Processing work item with value => 7
Thread# 14484: Work found. Taking one off the stack.
Thread# 14484: Processing work item with value => 6
Thread# 14944: Work found. Taking one off the stack.
Thread# 14944: Processing work item with value => 5
Thread# 5232: Work found. Taking one off the stack.
Thread# 5232: Processing work item with value => 4
Thread# 14484: Work found. Taking one off the stack.
Thread# 14484: Processing work item with value => 3
Thread# 14944: Work found. Taking one off the stack.
Thread# 14944: Processing work item with value => 2
Thread# 5232: Work found. Taking one off the stack.
Thread# 5232: Processing work item with value => 1
Thread# 14484: Work found. Taking one off the stack.
Thread# 14484: Processing work item with value => 7
Thread# 14944: Work found. Taking one off the stack.
Thread# 14944: Processing work item with value => 6
Thread# 5232: Work found. Taking one off the stack.
Thread# 5232: Processing work item with value => 5
Thread# 14484: Work found. Taking one off the stack.
Thread# 14484: Processing work item with value => 4
Thread# 14944: Work found. Taking one off the stack.
Thread# 14944: Processing work item with value => 3
Thread# 5232: Work found. Taking one off the stack.
Thread# 5232: Processing work item with value => 2
Thread# 14484: Work found. Taking one off the stack.
Thread# 14484: Processing work item with value => 1
Thread# 14944: Work found. Taking one off the stack.
Thread# 14944: Processing work item with value => 7
Thread# 5232: Work found. Taking one off the stack.
Thread# 5232: Processing work item with value => 6
Thread# 14484: Work found. Taking one off the stack.
Thread# 14484: Processing work item with value => 5
Thread# 14944: Work found. Taking one off the stack.
Thread# 14944: Processing work item with value => 4
Thread# 5232: Work found. Taking one off the stack.
Thread# 5232: Processing work item with value => 3
Thread# 14484: Work found. Taking one off the stack.
Thread# 14484: Processing work item with value => 2
Thread# 14944: Work found. Taking one off the stack.
Thread# 14944: Processing work item with value => 1
Add some more work
A
Thread# 5512: Work found. Taking one off the stack.
Thread# 5512: Processing work item with value => 7
Thread# 8520: Work found. Taking one off the stack.
Thread# 8520: Processing work item with value => 6
Thread# 15420: Work found. Taking one off the stack.
Thread# 15420: Processing work item with value => 5
Thread# 15532: Work found. Taking one off the stack.
Thread# 15532: Processing work item with value => 4
Thread# 14788: Work found. Taking one off the stack.
Thread# 14788: Processing work item with value => 3
Thread# 5512: Work found. Taking one off the stack.
Thread# 5512: Processing work item with value => 2
Thread# 8520: Work found. Taking one off the stack.
Thread# 8520: Processing work item with value => 1
]#