I want to add a feature to my Twinprimes code to create a visual cue that tells users how much of the process is left (what percentage of threads have been completed.)
I know how many threads are created for a sieve run, and just need to monitor|display in the main program how many have completed, until they all have run and finished.
I've been thinking of different ways to do this as simply as possible, with no|imperceptible performance loss.
I figured someone has done this kind of thing before and would like to see suggestions.
@jzakiya
this is example for inter-thread communication with channel
import std/random, threadpool
from os import sleep
const comptime = 10000
var chan: Channel[float]
chan.open
proc heavycomp =
var sleeping = 0
while sleeping < comptime:
let asleep = rand(comptime - sleeping)
sleeping += asleep
let percent = sleeping.float / comptime.float
sleep asleep
chan.send percent # send percentage to monitor thread
proc monitorcomp =
var percent = 0.0
while percent < 1.0:
percent = chan.recv # block current thread to wait the percent info
echo "Current comp at ", percent * 100, "%"
proc main =
randomize()
spawn(heavycomp())
spawn(monitorcomp())
sync()
main()