does a single waiting thread degrade performance?

BlitzMax Forums/BlitzMax Programming/does a single waiting thread degrade performance?

Rozek(Posted 2010) [#1]
Strange...

does the sheer presence of a separate thread (which does nothing but a "waitSemaphore") slow down the performance of a MT program?

I have two threads which communicate by sending each other some events - the event queues are protected using mutexes. The main thread also handles any GUI events (and thus periodically checks its reception queue) while the second thread simply uses "waitSemaphore" to wait until an event came in.

Mutex handling is independent of whether the second thread exists or not - but as soon as the second thread has been created (and just waits for its semaphore) the performance of the main thread goes down heavily(!). And this happens, although no events are currently transferred between the threads: the second thread just waits...

Just to make that clear again: there are only two threads (apart form those BMX creates itself) - one of them is waiting, and the other one (the main thread) handles GUI events. And it is this main thread, which seems to run only half as fast (roughly) when the second thread exists (and waits).

And that's bad...

Does anybody have similar experiences?


Rozek(Posted 2010) [#2]
Hmmm,

I have to investigate that further, but it could be related to the debugger: when compiled without the "debug build" option, the performance does not seem to suffer so much after starting a second thread. Thus, be aware of performance problems when debugging multi-threaded applications!


Rozek(Posted 2010) [#3]
Well,

it does NOT only be related to the debugger, it might be something else...

A question: will a thread be "closed" after its "body" function has "return"ed? Or is it ALWAYS necessary to "waithread" or "detachthread"?


Rozek(Posted 2010) [#4]
Ok, I got it!

It's IMPORTANT TO "detachThread" a thread even if it terminates itself by returning from the its "body" function! If you don't do that, you will soon run into trouble because your application will become veeeeeeeeerrrrrrrrrrrryyyyyyyyyy ssssllllllllllllllooooooooooooooooowwwwwwwwwwwwww!

Using the debugger also costs a lot of performance - but by far not as much as missing "detachThread" calls!


degac(Posted 2010) [#5]
Rozek I think you should write a tutorial some days - as there are no much documentation about threading in BlitzMax... and I think this will be very useful to many of us!


Rozek(Posted 2010) [#6]
Hmmm,

I could try and add some documentation to the BlitzMAX modules, at least.

If others look into the built-in docs as I do, they will immediately benefit from more detailled information.

I will just have to negotiate with Mark Sibly et al what might be the best way to share my insight (because I may be wrong with my assumptions about how MT works in BlitzMAX)