Patasuss

channels

May 19th, 2019
610
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import threadpool, os
  2.  
  3. type ComChannel = Channel[int]
  4. var channel : ComChannel
  5.  
  6. proc myFunc() =
  7.   while true:
  8.     let msg = channel.recv()
  9.     if msg == -1: #Quit thread
  10.       break
  11.     echo "Message: ", msg
  12.   echo "Quitting thread"
  13.  
  14. channel.open()
  15.  
  16. var t0 : Thread[void]
  17. t0.createThread(myFunc)
  18.  
  19. var a : seq[int]
  20. for i in 0..<10:
  21.   a.add(i)
  22. a.add(-1) #Thread quit signal
  23.  
  24. for i in a:
  25.   while not channel.trySend(i):
  26.     discard
  27.  
  28. channel.close()
  29. t0.joinThread()
  30.  
  31. # Sample output:
  32. # Sent: 0
  33. # Sent: 1
  34. # Sent: 2
  35. # Sent: 3
  36. # Sent: 4
  37. # Message: 0
  38. # Message: 1
  39. # Message: 2
  40. # Message: 3
  41. # Message: 4
  42. # Message: 5
  43. # Sent: 5
  44. # Sent: 6
  45. # Sent: 7
  46. # Sent: 8
  47. # Sent: 9
  48. # Sent: -1
  49. # Message: 6
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×