Multithreading

BlitzMax Forums/BlitzMax Programming/Multithreading

You don't need your code to be threadsafe. You should use thread-unsafe code whenever you can. You just need a way to lock the threads when a global variable is being written to. That is the only time the threads can affect one another, so you do something like this:

If SomethingSomething
LockThreads()
myvar=1
UnLockThreads()
EndIf

Locking threads slows the application because all threads must momentarily pause. A better way to handle this is to use arrays, with one value for each thread:

If SomethingSomething
myvar[ ThreadID() ]=1
EndIf

Calling GCSuspend() before a multi-threaded section might help.

However, my tests using BlitzMax callbacks with a multithreaded Newton simulation have always crashed unless I lock the threads.