Skip to content

Instantly share code, notes, and snippets.

@RklAlx
Created September 27, 2013 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RklAlx/6729814 to your computer and use it in GitHub Desktop.
Save RklAlx/6729814 to your computer and use it in GitHub Desktop.
Critical Section for Linux
// ---
// - Creating critical section variable -
// ---
pthread_mutex_t m;
// ---
// - Initializing -
//--
//create mutex attribute variable
pthread_mutexattr_t mAttr;
// setup recursive mutex for mutex attribute
pthread_mutexattr_settype(&mAttr, PTHREAD_MUTEX_RECURSIVE_NP);
// Use the mutex attribute to create the mutex
pthread_mutex_init(&m, &mAttr);
// Mutex attribute can be destroy after initializing the mutex variable
pthread_mutexattr_destroy(&mAttr);
// ---
// - Enter and Leave critical section -
// ---
pthread_mutex_lock (&m);
pthread_mutex_unlock (&m);
// ---
// - Destroy -
//--
pthread_mutex_destroy (&m);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment