Hi, I believe the below code does the same thing as you're trying to achieve, though I think I might be misunderstanding your aim - as it stands, there seems to be no reason to use threads whatsoever? import unicode, threadpool, locks, os proc lenU*(s: string): int = result = s.runeLen proc charAtPosU*(s: string, pos: int): string = assert(pos >= 0 and pos < s.runeLen) result = s.runeAtPos(pos).toUTF8() var lk: Lock proc printCharInString(r: Rune) = lk.acquire() defer: lk.release() stdout.write($r) proc multithreadedPrint(msg: string, iters: int) = let msgLen = msg.lenU numCalls = msgLen * iters if msgLen > 0: if msgLen > MaxDistinguishedThread: quit("Your string is too long. Maximum allowed is " & $MaxDistinguishedThread & "!") setMinPoolSize(msgLen) setMaxPoolSize(msgLen) initLock(lk) defer: deinitLock(lk) echo("Total threads: ", msgLen) echo("Total calls: ", numCalls) for i in 0..<iters: for r in msg.runes(): spawn printCharInString(r) sync() multithreadedPrint("0123456789", 2)
The output for a shorter (6 character) string is the same for my version and yours, and mine finishes with larger strings (such as your supplied 10 character one).