In what situations can the release std::unique_lock method be used? I made a mistake using the release method instead of the unlock method, and it took some time to figure out why the following code didn't work.
#include <mutex> #include <iostream> #include <vector> #include <thread> #include <chrono> std::mutex mtx; void foo() { std::unique_lock<std::mutex> lock(mtx); std::cout << "in critical section\n"; std::this_thread::sleep_for(std::chrono::seconds(1)); lock.release(); } int main() { std::vector<std::thread> threads; for (int i = 0; i < 5; ++i) threads.push_back(std::thread(foo)); for (std::thread& t : threads) t.join(); }
source share