I'm posting this here because I'm pretty sure it's not a bug, just something I don't understand. What am I doing wrong with the parallel statement here?
import threadpool, os
proc single(time: int) =
sleep time
echo time
proc sleepsort(nums: openArray[int]) =
var i = 0
parallel:
while i <= len(nums) + -1:
single(nums[i])
i += 1
sleepsort([50,3,40,25])
The error is this:
threadtest.nim(11, 18) Error: cannot prove: i <= len(nums) + -1
It seems that the while condition should be enough to prove that.
Well you need to define the 'i' in the parallel section, but even then the disjoint is so picky it doesn't compile. (Consider it reported.) However this works:
import threadpool, os
proc single(time: int) =
sleep time
echo time
proc sleepsort(nums: openArray[int]) =
parallel:
var i = 0
while i < len(nums):
spawn single(nums[i])
i += 1
sleepsort([50,3,40,25])
The simpler version works too:
import threadpool, os
proc single(time: int) =
sleep time
echo time
proc sleepsort(nums: varargs[int]) =
parallel:
for num in nums:
spawn single num
sleepsort 50,3,40,25
@def
That was my first approach but on version 0.10.2 the error is the same. I don't think anything posted in this thread actually compiles on that version, but yours and Araq's do if I compile the latest Nim from the repo.
@Araq
If that's true, then I'll actually need a different mechanism to start tasks. Is there a way to create a thread and stop it if it runs for too long? I've looked at the threads module in the standard library but there isn't a way to kill a thread in the documentation.
You can use createThread independently of spawn if a standalone thread is what you really need (spawn uses a thread pool).
You can't kill threads because there's no safe way to do so non-cooperatively using POSIX threads (POSIX thread cancellation is essentially cooperative). If you like living dangerously and know exactly what you're doing, you can use signals to interrupt a thread (on a POSIX system at least). But you had better understand precisely what you are doing, because such an interrupt can occur at almost any time and may leave the thread in an undefined state.
The best way to stop a thread if it runs too long is for it to check how long it's running and to abort if it's been running for too long.
just added mandelbrot fractal sample generated parallel mode, result is generated as a flat binary RGBA image (w x h)