Category : unique-lock

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. This is my code: #include <mutex> #include <iostream> using namespace std; std::mutex m; void fun2(); void fun1() { cout << "fun1" << endl; std::unique_lock<std::mutex> guard(m); cout << "lock mutex" << ..

Read more

Is there any potential problem if capturing a STD::unique_loc<std::mutex> in a lambda function which is used as a thread function? e.g: int foo() { std::mutex mtxRunOver; std::unique_lock <std::mutex> ulkRunOver(mtxRunOver); std::thread workThread([&](){ workThread.detach(); do_some_process_for_seconds(); } ); do_some_work(); } Source: Windows Que..

Read more

I use conditional_variable::notify_all() to wake up a waiting thread(only one thread is waiting for the unique_lock indeed). This code snippet works well at most time, but the log files(for details, see below) indicates that the parent thread could not aquire the unique_lock after the new created thread allready returned. I would be grateful to have ..

Read more

I am trying to use a shared recourse in the next way: A function inputs information into a string queue from file A function writes information from same queue to another file I made a class with private members: private: std::vector<std::string> _connectedUsers; std::condition_variable _cv; std::mutex _mtx; std::unique_lock<std::mutex> _lck; with the constructor: MessagesSender::MessagesSender() : _connectedUsers(), _cv(), ..

Read more

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 <iostream> #include <thread> #include <chrono> #include <mutex> std::condition_variable cv; std::mutex mtx; bool called = false; void printThread() { std::unique_lock<std::mutex> lck(mtx); while (std::cv_status::timeout== cv.wait_for(lck, std::chrono::seconds(1))) { std::cout << "*" ; } std::cout << "thread exits" << std::endl; } ..

Read more

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 <mutex> #include <thread> #include <chrono> #include <iostream> struct Box { explicit Box(int num) : num_things{num} {} int num_things; std::mutex m; }; void transfer(Box &from, Box &to, int ..

Read more

Here are two solutions of leetcode 1114 (concerning about mutex): class Foo { mutex mtx_1, mtx_2; unique_lock<mutex> lock_1, lock_2; public: Foo() : lock_1(mtx_1, try_to_lock), lock_2(mtx_2, try_to_lock) { } void first(function<void()> printFirst) { printFirst(); lock_1.unlock(); } void second(function<void()> printSecond) { lock_guard<mutex> guard(mtx_1); printSecond(); lock_2.unlock(); } void third(function<void()> printThird) { lock_guard<mutex> guard(mtx_2); printThird(); } }; class Foo ..

Read more