std::unique_lock::try_lock(3)
π NAME π
std::unique_lock::try_lock β std::unique_lock::try_lock. Bestie, this is the C++ Standard Library manual page for std::unique_lock::try_lock(3), and it's literally all about that non-blocking lock-attempt energy. β¨
πΈ Synopsis πΈ
bool try_lock(); (since C++11)
Okay so try_lock() tries to lock β i.e., takes ownership of β the associated mutex without blocking. Effectively calls mutex()->try_lock().
std::system_error is thrown if there is no associated mutex or if the mutex is already locked by this std::unique_lock.
π Parameters π
(none) β not a single parameter, bestie, we're traveling light. π
π¦ Return value π¦
true if the ownership of the mutex has been acquired successfully, false otherwise. Periodt. π«Ά
π Exceptions π
- Any exceptions thrown by
mutex()->try_lock()(Mutex types do not throw intry_lock, but a customLockablemight). - If there is no associated mutex,
std::system_errorwith an error code ofstd::errc::operation_not_permitted. - If the mutex is already locked by this
std::unique_lock,std::system_errorwith an error code ofstd::errc::resource_deadlock_would_occur.
No cap, those last two are the "you did what now?" errors. π
π§ Example π§
The following examples try to acquire a mutex that was locked and unlocked. Iconic behavior, honestly. π
// Run this code
#include <chrono>
#include <iostream>
#include <mutex>
#include <thread>
#include <vector>
using namespace std::chrono_literals;
int main()
{
std::mutex counter_mutex;
std::vector<std::thread> threads;
using Id = int;
auto worker_task = [&](Id id, std::chrono::seconds wait, std::chrono::seconds acquire)
{
// wait for a few seconds before acquiring lock.
std::this_thread::sleep_for(wait);
std::unique_lock<std::mutex> lock(counter_mutex, std::defer_lock);
if (lock.try_lock())
std::cout << '#' << id << ", lock acquired.\n";
else
{
std::cout << '#' << id << ", failed acquiring lock.\n";
return;
}
// keep the lock for a while.
std::this_thread::sleep_for(acquire);
std::cout << '#' << id << ", releasing lock (via destructor).\n";
};
threads.emplace_back(worker_task, Id{0}, 0s, 2s);
threads.emplace_back(worker_task, Id{1}, 1s, 0s);
threads.emplace_back(worker_task, Id{2}, 3s, 0s);
for (auto& thread : threads)
thread.join();
}
Output:
#0, lock acquired.
#1, failed acquiring lock.
#0, releasing lock (via destructor).
#2, lock acquired.
#2, releasing lock (via destructor).
π See also π
lockβ locks (i.e., takes ownership of) the associated mutex (public member function)try_lock_forβ attempts to lock (i.e., takes ownership of) the associatedTimedLockablemutex, returns if the mutex has been unavailable for the specified time duration (public member function)try_lock_untilβ tries to lock (i.e., takes ownership of) the associatedTimedLockablemutex, returns if the mutex has been unavailable until specified time point has been reached (public member function)unlockβ unlocks (i.e., releases ownership of) the associated mutex (public member function)
π Source/version: http://cppreference.com, 2024.06.10. That's the std::unique_lock::try_lock(3) manual page, bestie β mwah! π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms