Skip to content

Instantly share code, notes, and snippets.

@bluenote10
Created August 30, 2015 09:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bluenote10/d516ba98c5b1fb1bb8a2 to your computer and use it in GitHub Desktop.
Save bluenote10/d516ba98c5b1fb1bb8a2 to your computer and use it in GitHub Desktop.
Nim Channel Test
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()
@rogercloud
Copy link

Try joinThread(thread) after createThread, it works. So I guess it's TChannels concurrency bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment