Thread and CriticalSections

Started by
10 comments, last by hellfire 21 years, 8 months ago
I am useing the _beginthread function to create two threads. They both access the same object, but there is only one varible which is an int, that I want to make a critical section. What is the best and fastest way to to this?
Advertisement
I wasn''t aware that there was more than one way to create a critical section.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
It really depends on your intended purpose. You could use semaphores, mutexes, a monitor...if it''s an extremely simple case - where you''re trying to synchronize 2 threads, I''d suggest having a bolt variable.

critical section:

// ** bolt is a global boolean, which is seen by both threads
if( bolt )
{
bolt = false;
// ** do your work
bolt = true;
}

If you want more complicated, such as suspending a thread if it can''t enter the section, you should look into the operating system''s synchrnization functions and employ them. Given so little about your problem, that''s all I can suggest. G''luck!

People fear what they don''t understand, hate what they can''t conquer!
If you want to protect variables you''re probably better using a mutex. A critical section is to prevent the same bit of code being run in several threads concurrently.

Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
If you''re running on Windows use _beginthreadex instead of _beginthread. Only use _beginthread if your thread function uses the _cdecl calling convention - and if it does consider reworking it to use _stdcall instead.

For details see:
http://support.microsoft.com/default.aspx?scid=KB;EN-US;q132078&
http://www.microsoft.com/msj/0799/win32/win320799.htm
http://www.microsoft.com/msj/1099/win32/win321099.htm
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
How exactly do mutex work? I mean how do I implantment them in my code? The examples Ihave seen don''t seem to make sense.
Under Win32, a CriticalSection and a Mutex are very similar.
The only difference was that a Mutex can be given a name so that different processes can use it. Unless you need to synchronize across process boundaries, use a CriticalSection, which is faster.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
for simple thread safety, I use these 2 classes:


  // file: lockobj.h#include <windows.h>class CritSec : public CRITICAL_SECTION{public:  CritSec() { ::InitializeCriticalSection(this); }  ~CritSec() { ::DeleteCriticalSection(this); }};class Lock{public:  Lock(CRITICAL_SECTION& cs) : m_cs(&cs) { ::EnterCriticalSection(m_cs); }  ~Lock() { ::LeaveCriticalSection(m_cs); }private:  CRITICAL_SECTION* m_cs;};  



to use:


  void do_something(){  Lock l(g_cs); // g_cs is an instance of CritSec  // potentially thread-unsafe code here}  
daerid@gmail.com
quote:Original post by siaspete
If you want to protect variables you''re probably better using a mutex. A critical section is to prevent the same bit of code being run in several threads concurrently.

Uh.

Critical sections are for protecting variables too. The difference is that CSes are strictly per-process (and have a rather limited API; you can ininitalize, enter, leave, uninitialize them, and I think that''s all) whereas mutexes can be shared between processes and have the full set of WaitXxx() calls available.

Unless you need the extra functionality a mutex provides, a CS is a better choice, as they''re somewhat faster.

char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
quote:Original post by DrPizza
Unless you need the extra functionality a mutex provides, a CS is a better choice, as they're somewhat faster.


Not only "somewhat"

Grabbing a CS (~10 CPU instructions on x86) is much faster than a mutex, assuming only one thread tries to acquire the lock (there is no "contention"), because then the CS will operate entirely in usermode. EDIT: Using a mutex for locking always forces a transition to kernelmode.
If there is another thread waiting for the CS a transition to kernelmode is needed and the penalty for that is HIGH (~500 CPU instructions on x86). The kernelmode transition can luckily normally be avoided by using spinlocks and TryEnterCriticalSection().


quote:Original post by siaspete
If you want to protect variables you're probably better using a mutex. A critical section is to prevent the same bit of code being run in several threads concurrently.

You're right about CS, but the context gets incorrect.
There is no such thing as protecting a variable. You protect code. If you need to protect a variable this is done by protecting all accesses to the variable. Mutex and CS are two ways of doing that. A CS gives better performance, while the Mutext has other advantages such as working cross-process.
EDIT: I missed that DrPizza already had commented this, sorry about that.

A good article is: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndllpro/html/msdn_locktest.asp

[edited by - developer on August 15, 2002 6:40:08 PM]

This topic is closed to new replies.

Advertisement