When passing a 'seq' to a 'proc' that's been used for 'spawn'-ing a thread,
the 'seq' inside the new thread is immutable.
I found out that the 'seq' inside the new thread is already a physical copy
of the 'seq' inside the 'spawn'-ing thread.
But then I don't understand why the 'seq' inside the new thread needs to be immutable.
Could someone tell me please?
My intension is to modify such a 'seq' and 'spawn' new treads again
with the modified 'seq'.
Actually I assign the passed 'seq' to an additional local 'seq'.
That makes two copy actions per 'spawn'.
One done by the runtime system because of the 'spawn' and one done by me.
Thank you,
but the compiler denies.
Error: 'toTask'ed function cannot have a 'var' parameter
It's not as much about immutability. A var parameter is secretly a pointer, and this is problematic as the argument can go out of scope before the thread exits.
If the thread proc argument is passed by value, it should be safe to bypass the immutability via e.g.
proc doStuff(x: var seq[int]) =
x.add 3
# in this case, {.bycopy.} is redundant
proc myThread(x {.bycopy.}: seq[int]) {.thread.} =
var y = move (addr x)[]
doStuff(y)
But maybe the signature of createThread should really be
proc(t: var Thread[TArg], tp: proc(arg: sink TArg) {.nimcall, gcsafe.}, param: sink TArg)