FastPointer (multi-threading) Tips.

Blitz3D Forums/Blitz3D Userlibs/FastPointer (multi-threading) Tips.

MusicianKool(Posted 2010) [#1]
Just started working with the multi threading capabilities of fast pointer and figured out how to sync multiple threads within the main loop. Not sure if this has been figured out before but here it is:



Just noticed that :
If This = Null Then RuntimeError "Error In Thread: Type used in Thread was Deleted!"
inside 'MyFunction' doesn't do anything, in fact no kind of error control works inside a thread. I had all 10 threads still running with null types, odd yes. also it seems that you cannot free a thread from inside a thread, it must be done externally.


puki(Posted 2010) [#2]
This looks exciting.


MusicianKool(Posted 2010) [#3]
I have a question. How do you create a thread inside a thread, and a type inside the thread as well. I'm working on multi-threading my old theory of everything and it would be great if it could be done.


Yasha(Posted 2010) [#4]
Seems to work as expected (note that using a global as a lock like this is not actually thread-safe, it just happens to work - a generalised version of your example solution with objects would be better):



Why would that have been a problem?


MusicianKool(Posted 2010) [#5]
@Yasha: Thanks, The thing is it's hard to determine exactly when to many threads exist and when it's ok to create another thread. (your code works it's a good example of creating a thread inside a thread)

I guess for gaming purposes only a few threads are needed. For, like, scientific research, knowing how many threads are allowed on a system is needed so they don't crash a supercomputer. That's why I started this topic, to figure out when can be done and what can't be done. Tips.

hmm it seems as though there is a maximum number of threads that can be produced (unsafely of course ). Ie. My IBM p3 laptop does 1895 before it crashes, probably from using up to much memory.

Also I noticed that a thread calling a un-threaded function also makes it a temporary thread.
Example:


[edit] seems i am wrong.. only the last thread gets a new random after each pass ????
[edit] correction, seems that debug only shows the last threaded function updated, but still works just fine.


Yasha(Posted 2010) [#6]
Also I noticed that a thread calling a un-threaded function also makes it a thread.


??

I don't think you understand multithreading. There's no distinction between "threaded" and "unthreaded" functions in Blitz3D (and in fact in any other language it would be an unnecessary and arbitrary distinction too).

A second thread of execution is a second stack running in parallel to the first stack. Both can call all the functions in the program (although they may not work correctly). When you use CreateThread(), you aren't doing anything special to the function you call it with. All CreateThread() does is create the new thread of execution and start it running at the start of the function you pass to it. When that function calls others, they will run in that thread of execution too. When it returns, the thread is closed.

1895 threads is above-average for the entire system, and you really shouldn't be designing a single program to use that many. I think you really ought to be considering something like OpenCL if you need this kind of massively-parallel program structure as simple OS threads aren't normally used this way.

Finally... don't expect to get sensible results from Debug mode when testing threads (or anything else in FastPointer) - it wasn't designed to handle them so it can't give you accurate information.


MusicianKool(Posted 2010) [#7]
I c. that makes sense. Like I said, just started using multi- threading. And I'm not really trying to run a program with that many threads I'm just testing the limits of ,?? my system,?? Fast pointer,?? something.


Serpent(Posted 2010) [#8]
Since this is a multi-threading tips page, I'd like to ask a question: Is it 'thread-safe' to communicate between threads using global variables?
I think it would be useful if threads can 'communicate' as such so that they act like multiple programs and take advantage of multi-core processors.


MusicianKool(Posted 2010) [#9]
Serpent: Global's are not a very good way to communicate between threads. Here is an example:

Press space a couple times and see that threads access the global value at different points so the end value is uncertain to its exact value at any given point.



Now a safe way to communicate between threads is to create a communication type that links 2 treads, as either:
thread1 send-> received in thread2 - thread2 send -> received in thread3 .. ect.
or
2 threads that exclusively communicate between each other.

I'm guessing anyways, I'm working on an example for this.


MusicianKool(Posted 2010) [#10]
Here we go. With this example a thread can communicate with another thread.

For some reason this doesn't run in debug mode(at least on my system):




Serpent(Posted 2010) [#11]
Thanks for the example. Communication via types seems to be the best option for multiple threads.