C: How do you declare a recursive mutex with POSIX threads?

后端 未结 4 1180
忘了有多久
忘了有多久 2020-12-07 17:47

I am a bit confused on how to declare a recursive mutex using pthread. What I try to do is have only one thread at a time be able to run a piece of code(including functions)

4条回答
  •  借酒劲吻你
    2020-12-07 18:17

    On Linux (but this is non portable to other systems), if the mutex is a global or static variable, you could initialize it like

    static pthread_mutex_t recmutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
    

    (and by the way, the example is from pthread_mutex_init(3) man pages!)

提交回复
热议问题