4

I am coming from Java , so i am familiar with synchronize and not mutex. I wonder if pthread_mutex_t is also reentrancy. if not is there another mechanism for this?

Thank you

Avihai Marchiano
  • 3,837
  • 3
  • 38
  • 55
  • Good example to initialize here http://stackoverflow.com/questions/7037481/c-how-do-you-declare-a-recursive-mutex-with-posix-threads – JayS Jun 26 '15 at 21:28
  • 1
    If you need reentrant mutex there is usually problem with your program. – Slava Sep 02 '15 at 19:24

4 Answers4

11

This depends on the mutex type, the default does no checking and an attempt to lock it more than once in the same thread results in undefined behavior. Read about it here.

You can create a mutex of type PTHREAD_MUTEX_RECURSIVE to be able to recursively lock it, which is done by providing a pthread_mutexattr_t with the desired mutex type to pthread_mutex_init

nos
  • 223,662
  • 58
  • 417
  • 506
  • does mutex from #include (c++11) is the same? – Avihai Marchiano Jul 19 '12 at 19:45
  • 6
    Also note that recursive and reentrant are not the same thing. A recursive mutex is not necessarily reentrant, i.e. calling `pthread_mutex_lock` from a signal handler that interrupted `pthread_mutex_lock` invokes undefined behavior. It is however possible to implement `pthread_mutex_lock` in a reentrant way; see my implementation in musl libc for an example of how it can be done. The key detail is that a single atomic operation must transition the mutex from the unlocked state to the locked-with-count-1 state. – R.. GitHub STOP HELPING ICE Jul 19 '12 at 21:33
3

According to the manual, you can declare a mutex object as PTHREAD_MUTEX_RECURSIVE:

If the mutex type is PTHREAD_MUTEX_RECURSIVE, then the mutex shall maintain the concept of a lock count. When a thread successfully acquires a mutex for the first time, the lock count shall be set to one. Every time a thread relocks this mutex, the lock count shall be incremented by one. Each time the thread unlocks the mutex, the lock count shall be decremented by one. When the lock count reaches zero, the mutex shall become available for other threads to acquire. If a thread attempts to unlock a mutex that it has not locked or a mutex which is unlocked, an error shall be returned.

See also pthread_mutex_attr_settype.

Tudor
  • 61,523
  • 12
  • 102
  • 142
2

By default, pthread_mutex is not recursive, but there is a way for initialize it as recursive:

      pthread_mutexattr_t Attr;
      pthread_mutexattr_init(&Attr);
      pthread_mutexattr_settype(&Attr, PTHREAD_MUTEX_RECURSIVE);
      pthread_mutex_init(&_mutex, &Attr);
Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
JayS
  • 2,057
  • 24
  • 16
0

I think you don't mean reentrancy but recursiveness. Having reentrant locking with Java is impossible.
Recursive locking is simply when you already own a lock in a thread you might lock it again without any issues. Actually this is very efficient since this doesn't need any atomic operations but just a compare of the owning thread id of the mutex with the current thread id and if both are equal, an owning-counter is - non-atomically (!) - incremented.
Reentrancy is rather is asynchronous execution within the same thread. For example if you have a signal handler in C / C++ and this signal handler locks a mutex already owned by this thread in the synchronous part of the code again. I'm not aware if there are mutex implementations that can handle this situations but there's not much need for that so I guess not. I think such mutexes couldn't be as efficient as normal recursive mutexes.

Bonita Montero
  • 2,817
  • 9
  • 22