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

后端 未结 4 1220
忘了有多久
忘了有多久 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:18

    You need to add mutex attributes when creating the mutex.

    Call pthread_mutexattr_init, then pthread_mutexattr_settype with PTHREAD_MUTEX_RECURSIVE then use these attributes with pthread_mutex_init. Read man pthread_mutexattr_init for more info.

提交回复
热议问题