preparations for 0.8.12
This commit is contained in:
parent
2565ff8dde
commit
5b96eaa953
81 changed files with 2355 additions and 826 deletions
44
tests/threads/threadex.nim
Executable file
44
tests/threads/threadex.nim
Executable file
|
|
@ -0,0 +1,44 @@
|
|||
|
||||
type
|
||||
TMsgKind = enum
|
||||
mLine, mEof
|
||||
TMsg = object {.pure, final.}
|
||||
case k: TMsgKind
|
||||
of mEof: backTo: TThreadId[int]
|
||||
of mLine: data: string
|
||||
|
||||
var
|
||||
consumer: TThread[TMsg]
|
||||
producer: TThread[int]
|
||||
printedLines = 0
|
||||
|
||||
proc consume() {.thread.} =
|
||||
while true:
|
||||
var x = recv[TMsg]()
|
||||
if x.k == mEof:
|
||||
x.backTo.send(printedLines)
|
||||
break
|
||||
echo x.data
|
||||
discard atomicInc(printedLines)
|
||||
|
||||
proc produce() {.thread.} =
|
||||
var m: TMsg
|
||||
var input = open("readme.txt")
|
||||
while not endOfFile(input):
|
||||
if consumer.ready:
|
||||
m.data = input.readLine()
|
||||
consumer.send(m)
|
||||
close(input)
|
||||
m.k = mEof
|
||||
m.backTo = myThreadId[int]()
|
||||
consumer.send(m)
|
||||
var result = recv[int]()
|
||||
echo result
|
||||
|
||||
createThread(consumer, consume)
|
||||
createThread(producer, produce)
|
||||
joinThread(consumer)
|
||||
joinThread(producer)
|
||||
|
||||
echo printedLines
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue