Advertisement
Guest User

Untitled

a guest
Aug 31st, 2015
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. import os, threadpool, macros
  2.  
  3. template spawnBackgroundJob(t: typedesc, chan:ptr TChannel[t], iter: expr): stmt {.immediate.}=
  4. block:
  5. proc threadFunc(channel: ptr TChannel[t]) {.thread.} =
  6. echo "Thread is starting"
  7.  
  8. for i in iter:
  9. echo "Sending ", i
  10. channel[].send(i)
  11.  
  12. channel[].open()
  13.  
  14. var thread: TThread[ptr TChannel[t]]
  15. createThread(thread, threadFunc, chan)
  16. #joinThread(thread)
  17.  
  18.  
  19. # example use in some main thread:
  20. iterator testJob(): int =
  21. yield 0
  22. sleep(500)
  23. yield 1
  24. sleep(500)
  25. yield 2
  26.  
  27. var channel: ptr TChannel[int]
  28. channel = cast[ptr TChannel[int]](allocShared0(sizeof(TChannel[int])))
  29. spawnBackgroundJob(type(int), channel, testJob())
  30.  
  31. for i in 1 .. 10:
  32. sleep(200)
  33. echo channel[].peek()
  34.  
  35. channel[].close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement