C++ 参考手册
- C++11
- C++14
- C++17
- C++20
- C++ 编译器支持情况表
- 独立与宿主实现
- C++ 语言
- C++ 关键词
- 预处理器
- C++ 标准库头文件
- 具名要求
- 功能特性测试 (C++20)
- 工具库
- 类型支持(基本类型、RTTI、类型特性)
- 概念库 (C++20)
- 错误处理
- 动态内存管理
- 日期和时间工具
- 字符串库
- 容器库
- 迭代器库
- 范围库 (C++20)
- 算法库
- 数值库
- 输入/输出库
- 文件系统库
- 本地化库
- 正则表达式库
- 原子操作库
- 线程支持库
- std::thread
- std::stop_token
- std::stop_source
- std::stop_callback
- std::this_thread::get_id
- std::shared_timed_mutex
- std::shared_lock
- std::lock_guard
- std::hardware_destructive_interference_size, std::hardware_constructive_interference_size
- std::counting_semaphore, std::binary_semaphore
- std::jthread
- cpp/thread/barrier
- std::future
- std::this_thread::yield
- std::this_thread::sleep_for
- std::this_thread::sleep_until
- std::mutex
- std::recursive_mutex
- std::shared_mutex
- std::timed_mutex
- std::recursive_timed_mutex
- std::scoped_lock
- std::unique_lock
- std::defer_lock_t, std::try_to_lock_t, std::adopt_lock_t
- std::lock
- std::try_lock
- std::defer_lock, std::try_to_lock, std::adopt_lock
- std::once_flag
- std::call_once
- std::condition_variable
- std::condition_variable_any
- std::notify_all_at_thread_exit
- std::cv_status
- std::latch
- std::promise
- std::shared_future
- std::packaged_task
- std::async
- std::launch
- std::future_status
- std::future_error
- std::future_category
- std::future_errc
- 注释
- 实验性 C++ 特性
- 有用的资源
- 索引
- std 符号索引
- 协程支持 (C++20)
- C++ 关键词
std::try_lock
定义于头文件 <mutex>
|
||
template< class Lockable1, class Lockable2, class... LockableN> int try_lock( Lockable1& lock1, Lockable2& lock2, LockableN&... lockn); |
(C++11 起) | |
尝试锁定每个给定的可锁定 (Lockable) 对象 lock1
、 lock2
、 ...
、 lockn
,通过以从头开始的顺序调用 try_lock
。
若调用 try_lock
失败,则不再进一步调用 try_lock
,并对任何已锁对象调用 unlock
,返回锁定失败对象的 0
底下标。
若调用 try_lock
抛出异常,则在重抛前对任何已锁对象调用 unlock
。
参数
lock1, lock2, ... , lockn | - | 要锁定的可锁定 (Lockable) 对象 |
返回值
成功时为 -1
,否则为锁定失败对象的 0
底下标值。
示例
下列示例用 std::try_lock
周期地记录并重置运行于分离线程的计数器。
运行此代码
#include <mutex> #include <vector> #include <thread> #include <iostream> #include <functional> #include <chrono> int main() { int foo_count = 0; std::mutex foo_count_mutex; int bar_count = 0; std::mutex bar_count_mutex; int overall_count = 0; bool done = false; std::mutex done_mutex; auto increment = [](int &counter, std::mutex &m, const char *desc) { for (int i = 0; i < 10; ++i) { std::unique_lock<std::mutex> lock(m); ++counter; std::cout << desc << ": " << counter << '\n'; lock.unlock(); std::this_thread::sleep_for(std::chrono::seconds(1)); } }; std::thread increment_foo(increment, std::ref(foo_count), std::ref(foo_count_mutex), "foo"); std::thread increment_bar(increment, std::ref(bar_count), std::ref(bar_count_mutex), "bar"); std::thread update_overall([&]() { done_mutex.lock(); while (!done) { done_mutex.unlock(); int result = std::try_lock(foo_count_mutex, bar_count_mutex); if (result == -1) { overall_count += foo_count + bar_count; foo_count = 0; bar_count = 0; std::cout << "overall: " << overall_count << '\n'; foo_count_mutex.unlock(); bar_count_mutex.unlock(); } std::this_thread::sleep_for(std::chrono::seconds(2)); done_mutex.lock(); } done_mutex.unlock(); }); increment_foo.join(); increment_bar.join(); done_mutex.lock(); done = true; done_mutex.unlock(); update_overall.join(); std::cout << "Done processing\n" << "foo: " << foo_count << '\n' << "bar: " << bar_count << '\n' << "overall: " << overall_count << '\n'; }
可能的输出:
bar: 1 foo: 1 foo: 2 bar: 2 foo: 3 overall: 5 bar: 1 foo: 1 bar: 2 foo: 2 bar: 3 overall: 10 bar: 1 foo: 1 bar: 2 foo: 2 overall: 14 bar: 1 foo: 1 bar: 2 overall: 17 foo: 1 bar: 1 foo: 2 overall: 20 Done processing foo: 0 bar: 0 overall: 20
参阅
(C++11) |
锁定指定的互斥体,若任何一个不可用则阻塞 (函数模板) |