Created
September 27, 2013 14:50
-
-
Save RklAlx/6729814 to your computer and use it in GitHub Desktop.
Critical Section for Linux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// --- | |
// - 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