سؤال

According to POSIX, I can statically initialise a mutex this way:

pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;

However, what if I want the mutex to be recursive? Mutexes are non-recursive be default and there's no way to supply mutex attributes to the static initialisation.

هل كانت مفيدة؟

المحلول

It seems there is no portable way to do this. A workaround may be initialise the mutex dynamically when it is first used. To prevent race conditions while doing the initialisation another non-recursive statically initialised mutex can be used.

نصائح أخرى

Try :

pthread_mutex_t mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; 
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top