Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Author
Message
Sph!nx
16
Years of Service
User Offline
Joined: 3rd Dec 2008
Location: The Netherlands
Posted: 19th Oct 2013 22:34
Hey everybody,

My engine got quite a function (and will have a lot more of those) that will halt the complete program when done. They are (server side) "background" functions.

Now my question is if there is a way to have 2 loops at the same time, both not interfering each other, one that manages most (sync movement etc.) and the other for the background functions.

I've read about timer based movement, but my code is already quite extensive (server, client, playing server, etc.) and wish to try to avoid having to rewrite those... thus looking a way to completely separate parts from the general update.

Any help is much appreciated!

Regards Sph!nx
www.mental-image.net
Sasuke
19
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 19th Oct 2013 22:53
Well you can't have two loops to my knowledge, but depending on how you've modularized your program you can just call the functions at different rates using timer based movement. This way you won't have to rewrite them. But this depends what exactly there doing because TMB is a must is most cases.

"Get in the Van!" - Van B
Chris Tate
DBPro Master
16
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 20th Oct 2013 01:41 Edited at: 20th Oct 2013 01:47
Very true.

About the loops.

If you are talking about multi-threading, the closest you are going to get to that is using a plugin, a DLL, a seperate DBP app or two running side-by-side.

If you are talking about using a single threaded program with two major processes; then you can achieve that using one loop which triggers background tasks progressively; a bit like having a scheduled function call between your constant loop.

The other way to do this is by using more than one loop with Matrix1 coroutines. Each loop jumps to and from each other, instead of jumping back to their own loop keywords, they jump to the loop keywords of the other coroutine or sub-program. Each of these inner programs can choose whether to yield early or yield after some processing.

This is explained in a number of searchable examples in the forum; one of which I wrote which is a bit advanced.

Sph!nx
16
Years of Service
User Offline
Joined: 3rd Dec 2008
Location: The Netherlands
Posted: 20th Oct 2013 16:17
Thanks guys!

@ Sasuke
Yup, figured it out and implemented TBM. I have indeed made my program faster with a constant frame rate. Very nice.

Still, the function I was talking about still halts the app. I'm afraid I will have to rewrite that function so it will work in steps, instead of completing the function at once in the loop.

@ Chris
Nice one! Will take a look at the matrix coroutines before I will start to rewrite the function.

More suggestions are always welcome!

Regards Sph!nx
www.mental-image.net
Rudolpho
19
Years of Service
User Offline
Joined: 28th Dec 2005
Location: Sweden
Posted: 20th Oct 2013 17:02
Quote: "Still, the function I was talking about still halts the app."

What does this function do more exactly?
It may indeed be possible to run it in a separate thread if it can be isolated enough.


"Why do programmers get Halloween and Christmas mixed up?"
Sph!nx
16
Years of Service
User Offline
Joined: 3rd Dec 2008
Location: The Netherlands
Posted: 20th Oct 2013 17:21 Edited at: 20th Oct 2013 17:28
Well, two functions actually, and more will follow.

It's chunk management (server) and chunk mesh creation (client side)... I'm working with relative small chunks at the moment (16x16x16), thus 4096 blocks, all data compressed into a string, added in a membank.

So these strings need to be processed and even on my pc (which is pretty good) it halts the app a few seconds... this is for just one chunk creation.. imagine what time the generation of a sector will take!

I'm still thinking on how to best separate parts of the function, but some things must be done in a certain fashion or order and I wish to limit the unnecessary parsing of data.

More Details:
Server creates an "block Object" (array) on client request. This "block Object" holds a string with all membanks for each of it's chunks. Each chunk membank hold the string with all it;s block data.

Server generates a new chunk on client requests and sends data to client. Client accepts and creates the chunk mesh. (one mesh for each chunk, out of all it's blocks data). Both functions require a lot of processing, which halts the loop.

Regards Sph!nx
www.mental-image.net
Rudolpho
19
Years of Service
User Offline
Joined: 28th Dec 2005
Location: Sweden
Posted: 20th Oct 2013 18:09 Edited at: 20th Oct 2013 18:13
Ah, I see...
Well, you can't really multithread core DBPro functions such as creating objects as they aren't thread safe.
You could however have the other thread create the memblock layout (or whatever you're using) for the mesh and then do a much faster copying of that memory into the actual buffers from the main program.

I wrote a simple example of how you can create a separate thread to overcome the issue of blocking functions (in the example IanM's console input functionality). Note that this requires Mattrix1Utilities to compile:



Hope it helps


"Why do programmers get Halloween and Christmas mixed up?"
Sph!nx
16
Years of Service
User Offline
Joined: 3rd Dec 2008
Location: The Netherlands
Posted: 20th Oct 2013 18:39
Wow, thanks Rudolpho! Looks like what I need and I will study this very carefully.

Regards Sph!nx
www.mental-image.net
Rudolpho
19
Years of Service
User Offline
Joined: 28th Dec 2005
Location: Sweden
Posted: 20th Oct 2013 19:04
Quote: "rem I'll use it to write pixels to the shared buffer we set up earlier at regular intervals."

Hah, well that line is obviously not valid any longer. Initially I was thinking about having one thread draw things to a buffer and the other display it, but I thought the console was a better example since it actually solves the problem of it blocking the rest of your program.

Quote: "Wow, thanks Rudolpho!"

You're welcome.


"Why do programmers get Halloween and Christmas mixed up?"
Sph!nx
16
Years of Service
User Offline
Joined: 3rd Dec 2008
Location: The Netherlands
Posted: 20th Oct 2013 19:24 Edited at: 20th Oct 2013 19:24
Been searching on 'multithreading' (so that's what's it called!) and found a snippet by Ian M.



It's less complex to implement but it's more like hopping from one loop to another and then returning again... I'm glad that there are some options to achieve some kind of multithreading for the massive amount of data I need for the engine. (was really afraid I could not continue my game project.)

Regards Sph!nx
www.mental-image.net
Sph!nx
16
Years of Service
User Offline
Joined: 3rd Dec 2008
Location: The Netherlands
Posted: 22nd Oct 2013 18:46 Edited at: 22nd Oct 2013 18:46
Just to wrap this up for all who helped me.

I've used a combination of TMB and coroutines and got it fully implemented on the server. Works great without halting the app! I've got a flexible system that can reserve/add and end/remove coroutines. It works great! TMB has greatly improved the overall speed (and control) and stability.

I need to test it more and when I'm comfortable with it I will create an example snippet and post it here. I cannot post this now, cause it will be out of context, since my program is very complex (server, client, hosting player, dedicated mode...)

Thanks again to all! You made me very happy.

Regards Sph!nx
www.mental-image.net
Chris Tate
DBPro Master
16
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 22nd Oct 2013 20:12
Great; looking forward to seeing your work

Login to post a reply

Server time is: 2025-05-15 16:41:53
Your offset time is: 2025-05-15 16:41:53