std::try_lock

出自cppreference.com
< cpp‎ | thread
 
 
並發支持庫
線程
(C++11)
(C++20)
this_thread 命名空間
(C++11)
(C++11)
(C++11)
協作式取消
互斥
(C++11)
通用鎖管理
(C++11)
(C++11)
(C++11)
(C++11)
try_lock
(C++11)
條件變量
(C++11)
信號量
閂與屏障
(C++20)
(C++20)
未來體
(C++11)
(C++11)
(C++11)
(C++11)
安全回收
(C++26)
風險指針
原子類型
(C++11)
(C++20)
原子類型的初始化
(C++11)(C++20 棄用)
(C++11)(C++20 棄用)
內存定序
(C++11)(C++26 棄用)
原子操作的自由函數
原子標誌的自由函數
 
在標頭 <mutex> 定義
template< class Lockable1, class Lockable2, class... LockableN >
int try_lock( Lockable1& lock1, Lockable2& lock2, LockableN&... lockn );
(C++11 起)

嘗試鎖定每個給定的可鎖定 (Lockable) 對象 lock1lock2...lockn,從頭開始依次調用 try_lock

若調用 try_lock 失敗,則不再繼續調用 try_lock,並對任何已鎖對象調用 unlock,返回鎖定失敗對象的基於 0 的索引。

若調用 try_lock 拋出異常,則在重拋前對任何已鎖對象調用 unlock

目錄

[編輯] 參數

lock1, lock2, ... , lockn - 要鎖定的可鎖定 (Lockable) 對象

[編輯] 返回值

成功時為 -1,否則為鎖定失敗對象的基於 0 的索引值。

[編輯] 示例

下列示例用 std::try_lock 周期地記錄並重置運行於分離線程的計數器。

#include <chrono>
#include <functional>
#include <iostream>
#include <mutex>
#include <thread>
#include <vector>
 
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 << "处理完成\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
处理完成
foo: 0
bar: 0
overall: 20

[編輯] 參閱

(C++11)
鎖定指定的互斥體,若任何一個不可用則阻塞
(函數模板) [編輯]