Created
August 30, 2015 09:47
-
-
Save bluenote10/d516ba98c5b1fb1bb8a2 to your computer and use it in GitHub Desktop.
Nim Channel Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, threadpool | |
proc spawnBackgroundJob[T](channel: ptr Channel[T], f: iterator (): T) = | |
type Args = tuple[iter: iterator (): T, channel: ptr Channel[T]] | |
proc threadFunc(args: Args) {.thread.} = | |
echo "Thread is starting" | |
let iter = args.iter | |
var channel = args.channel # note: still a `ptr Channel` | |
for i in iter(): | |
echo "Sending ", i | |
channel[].send(i) | |
channel[].open() | |
var thread: Thread[Args] | |
let args = (f, channel) | |
createThread(thread, threadFunc, args) | |
# example use in some main thread: | |
iterator testJob(): int {.closure.} = | |
yield 0 | |
sleep(500) | |
yield 1 | |
sleep(500) | |
yield 2 | |
var channel: Channel[int] | |
spawnBackgroundJob(channel.addr, testJob) | |
for i in 1 .. 10: | |
sleep(200) | |
echo channel.peek() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Try joinThread(thread) after createThread, it works. So I guess it's TChannels concurrency bug.