简体   繁体   English

linux中的pthread_mutex_t是否是可重入的(如果一个线程试图获取它已经持有的锁,则请求成功)

[英]Does pthread_mutex_t in linux are reentrancy (if a thread tries to acquire a lock that it already holds, the request succeeds)

I am coming from Java , so i am familiar with synchronize and not mutex.我来自 Java,所以我熟悉同步而不是互斥锁。 I wonder if pthread_mutex_t is also reentrancy.我想知道 pthread_mutex_t 是否也是可重入的。 if not is there another mechanism for this?如果没有,还有另一种机制吗?

Thank you谢谢

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您可以创建PTHREAD_MUTEX_RECURSIVE类型的互斥锁,以便能够递归锁定它,这是通过向pthread_mutex_init提供具有所需互斥锁类型的pthread_mutexattr_t来完成的

According to the manual , you can declare a mutex object as PTHREAD_MUTEX_RECURSIVE:根据手册,您可以将互斥对象声明为 PTHREAD_MUTEX_RECURSIVE:

If the mutex type is PTHREAD_MUTEX_RECURSIVE, then the mutex shall maintain the concept of a lock count.如果互斥锁类型是 PTHREAD_MUTEX_RECURSIVE,则互斥锁应维护锁计数的概念。 When a thread successfully acquires a mutex for the first time, the lock count shall be set to one.当线程第一次成功获取互斥锁时,锁计数应设置为 1。 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 .另请参见pthread_mutex_attr_settype

By default, pthread_mutex is not recursive, but there is a way for initialize it as recursive:默认情况下, pthread_mutex不是递归的,但有一种方法可以将其初始化为递归:

      pthread_mutexattr_t Attr;
      pthread_mutexattr_init(&Attr);
      pthread_mutexattr_settype(&Attr, PTHREAD_MUTEX_RECURSIVE);
      pthread_mutex_init(&_mutex, &Attr);

I think you don't mean reentrancy but recursiveness.我认为您的意思不是可重入性,而是递归性。 Having reentrant locking with Java is impossible.使用 Java 进行可重入锁定是不可能的。
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.实际上这是非常有效的,因为这不需要任何原子操作,只需将互斥锁的拥有线程 id 与当前线程 id 进行比较,如果两者相等,则拥有计数器是 - 非原子的(!) -递增。
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.例如,如果您在 C/C++ 中有一个信号处理程序,并且此信号处理程序再次在代码的同步部分锁定该线程已经拥有的互斥锁。 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.我认为这样的互斥锁不能像普通的递归互斥锁那样高效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM