Multithreading with GLBasic [official]

Previous topic - Next topic

dreamerman

Wasn't aware of OpenMP, very handy for splitting huge calculations from one loop to multiple cores without bothering to much, could use that in something else.
Yeah I know parallel threads are tricky, specially for such things that are shared by different threads, good design will be crucial to get it working properly and efficiently, this is some kid of final goal, most likely at start I will stick with single thread for some time (do most game logic to get working demo or something), but MT would be final goal. Do you still have this example project with lib somewhere?
Check my source code editor for GLBasic - link Update: 20.04.2020

Kitty Hello

I attached the original project in the first post. Sorry, I didn't see it was deleted.

nabz32

Is there some example how to use the threads?
I was last using threads with posix threads back in 2017.
What I want to achieve is loading data in a seperate thread and continue refreshing the screen in the main thread. Of course I wont access the ressources that are loaded from the main thread while it is loading.
So basically I saw how to create a thread, but how to delegate work to the thread, send an interupt from the side thread to the main thread etc.?

nabz32

Nevermind, I found a good tutorial on hawk threads, I will give it a try sometime

Kitty Hello

Did you find the library? If so, multithreading is realitively easy. When you only read data, everything is fine. Once you start writing bits, you must ensure, that no other thread tries to read or write to the same memory location at the very same time. You do this, by setting locks (critical section or mutex in HawkThreads). The main problem is to organize your work loops, so you don't have a lot of locks.
You will get used to it, when you start doing things in parallel. Often, I use separate lists for the result of each thread and merge the results after the parallel code part together to one result.

nabz32

I have not searched for the library yet,

I want to load 3d Objects while only displaying the 2D buffer, is this thread safe, or are there some overlaps in memory?

Also reading and saving files in the background would be nice, since I added the collectables to the save files, saving lag has become noticeable ingame.

Kitty Hello

Oh. The GLBasic commands are not considered thread safe. Espeically graphics commands. Where is the bottleneck? Is it really the loading?
LOADSPRITE and X_LOADOBJ calls can't be multithreading. But drawing while loading other stuff might be - as long as you don't reload over existing channels.

nabz32

But could I access plain files in the background ( just writing stuff)?

Kitty Hello

Yes, that should work. Multithreadingbreaks, when you write to a memory address and another thread tries to read/write to the same memory address at the same time. Parallel reading is no problem. But internal GLBasic functions often use a "cache" for stuff to be quicker. That might get you into trouble. Just try, and if it breaks, add a critical section around that code.

nabz32

OK, I just need to make sure, that it doesn't save again, while a background save is in progress. Escape key is already disabled if not in debug mode, so it should be safe, If someone task kills the game while the save is running it shall not my problem