Questions tagged [recursive-mutex]

A recursive mutex (reentrant mutex) is a mutex which may be locked multiple times by the same process or thread, without causing a deadlock.

A recursive mutex (reentrant mutex) is a mutex which may be locked multiple times by the same process or thread, without causing a deadlock.

See also:

41 questions
205
votes
8 answers

Recursive Lock (Mutex) vs Non-Recursive Lock (Mutex)

POSIX allows mutexes to be recursive. That means the same thread can lock the same mutex twice and won't deadlock. Of course it also needs to unlock it twice, otherwise no other thread can obtain the mutex. Not all systems supporting pthreads also…
Mecki
  • 125,244
  • 33
  • 244
  • 253
77
votes
8 answers

When to use recursive mutex?

I understand recursive mutex allows mutex to be locked more than once without getting to a deadlock and should be unlocked the same number of times. But in what specific situations do you need to use a recursive mutex? I'm looking for…
jasonline
  • 8,646
  • 19
  • 59
  • 80
59
votes
3 answers

std::mutex vs std::recursive_mutex as class member

I have seen some people hate on recursive_mutex: http://www.zaval.org/resources/library/butenhof1.html But when thinking about how to implement a class that is thread safe (mutex protected), it seems to me excruciatingly hard to prove that every…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
56
votes
4 answers

How do you declare a recursive mutex with POSIX threads?

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) but after scepticism I figured out that the use of mutexes would not work…
Pithikos
  • 18,827
  • 15
  • 113
  • 136
31
votes
2 answers

Can you combine std::recursive_mutex with std::condition_variable?

Can you combine std::recursive_mutex with std::condition_variable, meaning do something like this: std::unique_lock lock(some_recursive_mutex) some_condition_var.wait(lock); If it's not allowed, then why not? I am using…
emesx
  • 12,555
  • 10
  • 58
  • 91
17
votes
1 answer

Can unique_lock be used with a recursive_mutex?

According the this, unique_lock can be used for recursive locking by declaring a std::unique_lock, and in fact that compiles fine. However, it appears from examining the code (gcc 4.8.2 and 4.9.0) that unique_lock doesn't defer…
WallStProg
  • 391
  • 4
  • 8
7
votes
1 answer

Access the owners counter used by std::recursive_mutex

I have a case where my algorithm's decisions are based on the depth of a shared std::recursive_mutex. #include #include #include int g_i = 0; std::recursive_mutex g_i_mutex; void bar() { …
DsCpp
  • 2,259
  • 3
  • 18
  • 46
5
votes
2 answers

Does pthreads support a method for querying the "lock count" of a recursive mutex?

Does pthreads support any method that allows you to query the number of times a recursive mutex has been locked?
dicroce
  • 45,396
  • 28
  • 101
  • 140
5
votes
3 answers

How to use recursive QMutex

I'm trying to use a recursive QMutex, i read the QMutex Class Reference but i not understand how to do it, can someone give me an example? I need some way to lock QMutex that can be unlocked after or before the lock method is called. If recursive…
user1576869
4
votes
2 answers

Is this an appropriate use case for a recursive mutex?

I've heard from various sources (1, 2) that one should avoid using recursive mutexes as it may be a sign of a hack or bad design. Sometimes, however, I presume they may necessary. In light of that, is the following an appropriate use case for a…
adyavanapalli
  • 490
  • 6
  • 17
4
votes
1 answer

Moving a unique_lock to another thread

I was wondering what happens when you move a unique_lock that holds a recursive_mutex. Specifically, I was looking at this code: recursive_mutex g_mutex; #define TRACE(msg) trace(__FUNCTION__, msg) void trace(const char* function, const char*…
Mr. Anderson
  • 1,609
  • 1
  • 13
  • 24
4
votes
4 answers

Recursive and Non-Recursive Locks (Mutex)

I am having problems with deadlocking in my program. So I have been reading about the locks, but the problem is most information is inconsistent or not platform defined. At the Recursive Lock (Mutex) vs Non-Recursive Lock (Mutex) the most accepted…
Rodrigo Rutsatz
  • 285
  • 5
  • 11
4
votes
2 answers

Idea Behind Recursive Mutex Lock

I'm working on a school lab and we are instructed to create a recursive mutex lock for a counting program. I've written some code (which doesn't work), but I think that this is mostly because I do not understand the real idea behind using a…
Davis
  • 109
  • 1
  • 7
4
votes
2 answers

Behavior of condition_variable_any when used with a recursive_mutex?

When using condition_variable_any with a recursive_mutex, will the recursive_mutex be generally acquirable from other threads while condition_variable_any::wait is waiting? I'm interested in both Boost and C++11 implementations. This is the use case…
Emile Cormier
  • 28,391
  • 15
  • 94
  • 122
3
votes
1 answer

Callbacks and `std::recursive_mutex` - valid use case?

I have the following polymorphic interface: struct service { virtual void connect(std::function(bool) cb); // Invoke 'cb' with 'true' on connection success, 'false' otherwise. virtual ~service() { } }; Some implementations of…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
1
2 3