Questions tagged [unique-lock]

The class std::unique_lock is a general-purpose mutex ownership wrapper allowing deferred locking, time-constrained attempts at locking, recursive locking, transfer of lock ownership, and use with condition variables.

32 questions
12
votes
2 answers

condition_variable wait_for in C++

I am working with condition_variable on Visual studio 2019. The condition_variable.wait_for() function returns std::cv_status::no_timeout without any notification. #include #include #include #include…
lfybt
  • 123
  • 1
  • 5
5
votes
2 answers

Two std::unique_lock used on same mutex causes deadlock ?

I found this code on code review stack exchange which implements a producer-consumer problem. I am posting a section of code here. In the given code, let's consider a scenario when producer produces a value by calling void add(int num), it acquires…
Sujith
  • 107
  • 6
4
votes
2 answers

Does std::lock_guard release the mutex after constructed with std::adopt_lock option?

I know my question is quite similar to this Why does std::lock_guard release the lock after using std::adopt_lock?, but the behavior I see is not. Here is my code: #include #include using namespace std; std::mutex m; void…
Huy Nhat Tran
  • 135
  • 11
4
votes
1 answer

Can using the lock of a mutex, adopted by a lock_guard, lead to a UB?

Can the following snippet cause an undefiled behaviora due to using the lock of the mutex already adopted by a lock_guard? and will it be safe if I use unique_lock instead of lock_guard in the same snippet? I know that there are…
asmmo
  • 6,922
  • 1
  • 11
  • 25
3
votes
1 answer

Use multiple std::unique_lock on mutex, all threads in FIFO to wait process?

When I have three threads or more, if mutex unlock in one thread, which one will be the next to process? They are in FIFO rule? If not FIFO, several thread wait unlock(), will have a thread never process? Do they wait in a sorted queue and what is…
3
votes
2 answers

Passing mutex reference from main to a class

I need to work with the same mutex and unique_lock across the main function and class instances. However, I am having trouble assigning the mutex/unique_lock address to a class member variable (that is a mutex&). This is what I have: Worker.h class…
Andrej
  • 35
  • 1
  • 4
2
votes
1 answer

Producer and Consumer Problem waiting threads

I tried to code Producer and Consumer Problem but after producing up to a certain value -(val) produce thread ends. but the consumer thread's condition variable waiting for notification but there is no thread to notify so my program does not end.…
2
votes
1 answer

Does this unique_lock in dtor serve any purpose?

Ran across this destructor in a codebase I am debugging. ManagerImpl::~ManagerImpl() { // don't go away if some thread is still hitting us boost::unique_lock l(m_mutex); } Does it actually serve any useful purpose in a…
Cinder Biscuits
  • 4,880
  • 31
  • 51
2
votes
0 answers

Using the same mutex for unique_lock and scoped_lock

Is it appropriate to use both a unique_lock and a scoped_lock with the same mutex? To allow for use of cv.wait and optional unlocking while also providing scope-bound safety. For example; std::mutex mut; //thread: std::condition_variable…
Mercer
  • 148
  • 1
  • 10
1
vote
1 answer

waiting threads status while mutex gets released at the end of a loop in wait_for function in C++

Suppose we have four threads in this order (D, C, B, A) waiting in a queue, and 'r.cv.notify_all()' just gets invoked, and suppose thread A (first thread in the queue) locks the mutex and the lambda returns true, after thread A reaches the end of…
Sami
  • 513
  • 4
  • 11
1
vote
1 answer

Why does std::condition_variable wait() require a std::unique_lock arg?

My thread does not need to be locked. std::unique_lock locks thread on construction. I am simply using cond_var.wait() as a way to avoid busy waiting. I have essentially circumvented the auto-locking by putting the unique_lock within a tiny scope…
1
vote
1 answer

what does unique_lock mean when a single thread acquire 2 unique_lock of the same mutex?

I have the following code, which is from https://en.cppreference.com/w/cpp/thread/unique_lock. However, upon, printing the output, I see some unexpected result and would like some explaination. The code is: #include #include…
Minh Pham
  • 275
  • 1
  • 13
1
vote
1 answer

Is this code exceptions handled correctly?

In the following code, it is possible that event throws exception and it may be not handled in even handler, (rare but its still the case) I want keep "lck2" unlocked while executing the event, because I don't want main thread block for "mtx2",…
M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118
0
votes
1 answer

How can two std::unique_locks simultaneously own a lock on the same mutex?

Currently I'm looking at a situation where an application gets stuck while two distinct instances of std::unique_lock in two separate threads simultaneously "own" a lock on the same std::mutex. Owning in this case means that owns_lock() returns true…
norritt
  • 345
  • 4
  • 16
0
votes
1 answer

Return std::move(unique_lock<>)

When I read the book "C++ in concurrent",I see the code about return the std::move(unique_lock<>), for example: std::unique_lock wait_for_data() { std::unique_lock head_lock(head_mutex); …
ppka
  • 11
1
2 3