std::try_lock
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <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
的第一. 原文:
Tries to lock each of the given
Lockable
objects lock1
, lock2
, ...
, lockn
by calling try_lock
in order beginning with the first. 如果调用
try_lock
失败,unlock
任何被锁定的对象被称为返回一个0
的索引,无法锁定的对象.....原文:
If a call to
try_lock
fails, unlock
is called for any locked objects and a 0
-based index of the object that failed to lock is returned.如果
try_lock
结果调用的异常,unlock
要求任何被锁定的对象,然后重新抛出.原文:
If a call to
try_lock
results in an exception, unlock
is called for any locked objects before rethrowing.目录 |
[编辑] 参数
lock1, lock2, ... , lockn | - | Lockable 对象锁定 |
[编辑] 返回值
-1
成功,或0
无法锁定的对象的索引值.原文:
-1
on success, or 0
-based index value of the object that failed to lock.[编辑] 示例
下面的示例使用
std::try_lock
定期理货和重置计数器在单独的线程中运行.
原文:
The following example uses
std::try_lock
to periodically tally and reset counters running in separate threads.
#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'; }
Possible output:
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) |
指定的锁互斥体,块,如果有任何无法使用 原文: locks specified mutexes, blocks if any are unavailable (函数模板) |