What are gcc on linux's equivalent to microsoft's critical sections? What are gcc on linux's equivalent to microsoft's critical sections? multithreading multithreading

What are gcc on linux's equivalent to microsoft's critical sections?


On Linux (and other Unixen) you need to use PThreads, or Posix Threads. There is no equivalent to Critical Sections on Windows; use a Mutex instead.

EDIT: See first comment below -- apparently Posix Mutexes are the same as Win32 Critical Sections in that they are bound to a single process.


Check here: http://en.wikipedia.org/wiki/Critical_section

/* Sample C/C++, Unix/Linux */#include <pthread.h>/* This is the critical section object (statically allocated). */static pthread_mutex_t cs_mutex =  PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;void f(){    /* Enter the critical section -- other threads are locked out */    pthread_mutex_lock( &cs_mutex );    /* Do some thread-safe processing! */    /*Leave the critical section -- other threads can now pthread_mutex_lock()  */    pthread_mutex_unlock( &cs_mutex );}int main(){    f();    return 0;}


EnterCriticalSection and the rest of the APIs are Win32 APIs. As far as cross-platform synchronization APIs, I don't think there are any (since you mention you can't use boost). Also, you mentioned cross-platform, does this mean different architectures too (for the gcc part i.e.).I've seen one large implementation where there was a common set of APIs provided which were conditionally compiled to have the native APIs (like fetch_and_add on AIX) or used pthreads the Win32 APIs.I once tried to use posix threads on win32 but ran into a bunch of issues (but that was a very old version). Now YMMV.