Advertisement

Mutex? Critical sections?

Started by September 05, 2002 08:33 PM
2 comments, last by Max_Payne 22 years, 5 months ago
Can somebody explain the meaning of these (something short). I''m currently simply creating one listen thread for my server and one for my client, i ain''t getting synchronisation problems currently, i''m using _beginthreadex and _endthreadex. Can somebody also tell me how to stop a thread from the outside.... (is there a way to KILL the thread instead of ending it from the inside?!).

Looking for a serious game project?
www.xgameproject.com
one thing i''d recommend you do is check out www.boost.org as they have fantastic thread libraries for your use, and they are well documented etc...

mutex/critical sections are all in there

-jonnii

-jonnii=========jon@voodooextreme.comwww.voodooextreme.com
Advertisement
A CRITICAL_SECTION is quicker than a mutex. The major difference between the two is that fact that a mutex support multi-processes, while a CRITICAL_SECTION supports single process only.

Kuphryn
The critical section and mutex are both used to synchronize access to a resource. For example, let''s assume you have a thread that increments a global variable from zero to five, then outputs the result. You don''t want a second thread to step in part way through this process and decrement that variable, otherwise the first thread won''t output the correct value. The solution is to guard access to that variable with a synchronization object, like a critical section, mutex or semaphore. The second thread then won''t be able to use the variable until the first thread has finished with it.

A critical section is faster than a mutex because it exists in user mode. A mutex exists in kernel mode, so every time you go to use it, Windows has to make a kernel mode transition, which is slow! However a mutex is useful because it can be used by more than one process at the same time. This might be handy if you wanted to synchronize access to something like a serial port.

You can tell a thread to stop by using the Windows TerminateThread API function. This is not, however, a good idea, since it doesn''t give the thread a chance to clean up after itself. You''re better off trying to signal the thread that it''s time to exit. Take a look at the Windows CreateEvent API function in the platform SDK documentation.

This topic is closed to new replies.

Advertisement