Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import threadpool, os
- type ComChannel = Channel[int]
- var channel : ComChannel
- proc myFunc() =
- while true:
- let msg = channel.recv()
- if msg == -1: #Quit thread
- break
- echo "Message: ", msg
- echo "Quitting thread"
- channel.open()
- var t0 : Thread[void]
- t0.createThread(myFunc)
- var a : seq[int]
- for i in 0..<10:
- a.add(i)
- a.add(-1) #Thread quit signal
- for i in a:
- while not channel.trySend(i):
- discard
- channel.close()
- t0.joinThread()
- # Sample output:
- # Sent: 0
- # Sent: 1
- # Sent: 2
- # Sent: 3
- # Sent: 4
- # Message: 0
- # Message: 1
- # Message: 2
- # Message: 3
- # Message: 4
- # Message: 5
- # Sent: 5
- # Sent: 6
- # Sent: 7
- # Sent: 8
- # Sent: 9
- # Sent: -1
- # Message: 6
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement