I've been trying to get multithreading to work with a shared variable, but for
some reason can't make it work. I've tried multiple things, but the following
code is the closest I got to what I'm trying to achieve.
So I tried to define a global variable with allocShared0 of type `seq[string]`.
Then I created a function called `myThread()` which will be running in a new
thread, from which I would like to access my global variable. By passing the
pointer to the global variable as parameter to the myThread function, I was
able to read the first value in the sequence by dereferencing the pointer it in
the new thread.
However, adding a new entry to this seq and then trying to access the data from
the main thread does not seem to work for me.
#import system/threads
import os
var data = cast[seq[string]](allocShared0(sizeof(seq[string])))
data.add("Test")
proc myThread(test : ptr seq[string]) {.thread.} =
echo "Start of new thread"
echo test[][0]
test[].add("I want to access this from the main thread!")
echo "End of new thread"
echo "Start"
echo type(data.addr)
var thread: Thread[ptr seq[string]]
createThread[ptr seq[string]](thread,myThread,data.addr)
joinThreads(thread)
echo "Thread done. Trying to access second string from seq:"
sleep(500)
echo data.addr[][1]
Run
PS: I also have another small issue in VSCode which shows errors on the three
lines with Thread, createThread and joinThreads when I do not use `import
system/threads`. However, when compiling without `import system/threads`, I get
no errors. Additionally, when I do add `import system/threads`, VSCode doesn't
show any errors, but when compiling it I get the following error: `D:\Program
Files\nim-1.6.6\lib\system\threads.nim(48, 10) Error: You must not import this
module explicitly` Not really blocking, but kinda annoying to permanently have
12 errors in my code which are not there... E.g.:
* undeclared identifier: 'Thread'
* no generic parameters allowed for Thread
* expression 'createThread' has no type (or is ambiguous)
* undeclared identifier: 'createThread'
* expression 'createThread[ptr seq[string]]' cannot be called
* ...