Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os, threadpool, macros
- template spawnBackgroundJob(t: typedesc, chan:ptr TChannel[t], iter: expr): stmt {.immediate.}=
- block:
- proc threadFunc(channel: ptr TChannel[t]) {.thread.} =
- echo "Thread is starting"
- for i in iter:
- echo "Sending ", i
- channel[].send(i)
- channel[].open()
- var thread: TThread[ptr TChannel[t]]
- createThread(thread, threadFunc, chan)
- #joinThread(thread)
- # example use in some main thread:
- iterator testJob(): int =
- yield 0
- sleep(500)
- yield 1
- sleep(500)
- yield 2
- var channel: ptr TChannel[int]
- channel = cast[ptr TChannel[int]](allocShared0(sizeof(TChannel[int])))
- spawnBackgroundJob(type(int), channel, testJob())
- for i in 1 .. 10:
- sleep(200)
- echo channel[].peek()
- channel[].close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement