How does mutex scope work exactly.
If I want 3 mutexes for different things and place them as so
static pthread_mutex_t watchdogMutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t watchdogCond = PTHREAD_COND_INITIALIZER;
int waitingForGpsSetupThread = 1;
static pthread_mutex_t gpsRunningMutex = PTHREAD_MUTEX_INITIALIZER;
int gpsRunning = 0;
static pthread_mutex_t indoorNavigationRunningMutex = PTHREAD_MUTEX_INITIALIZER;
int indoorSystemRunning = 0;
Are the variables defined within the scope of the first above mutex declaration or how does it work?
As written, the three mutexes are all in the same scope. There are no blocks marked by ‘{…}’ to indicate otherwise. The same would be true if the types were all
int. From that point of view, a mutex is no different from any other type.At the point of use, you would do something like:
The bit in the middle could be said to be the scope in which the mutex is locked. It would be a very bad idea to have a
returnstatement in the middle of those operations – unless the mutex was also unlocked before returning.See the POSIX definitions.