C++11多线程之lock类型

原文:
http://en.cppreference.com/w/cpp/thread/lock_tag_t
http://en.cppreference.com/w/cpp/thread/lock_tag

lock类型: std::defer_lock_t, std::try_to_lock_t, std::adopt_lock_t

定义在头文件 中。
- struct defer_lock_t {}; (since C++11)
- struct try_to_lock_t {}; (since C++11)
- struct adopt_lock_t {}; (since C++11)

std::defer_lock_t, std::try_to_lock_t, std::adopt_lock_t 是空的结构体,用来起到一个标记的作用,可以被用于 std::lock_guard, std::unique_lock, 和 std::shared_lock(C++14).

类型 实例 效果
defer_lock_t defer_lock 不会立即获得mutex的所有权
try_to_lock_t try_to_lock 尝试获得mutex的所有权,而不会阻塞
adopt_lock_t adopt_lock 假设调用者线程已经获得mutex的所有权

示例程序:

#include <mutex>
#include <thread>
#include <iostream>

struct bank_account {
    explicit bank_account(int balance) : balance(balance) {}
    int balance;
    std::mutex m;
};

void transfer(bank_account &from, bank_account &to, int amount)
{
    // lock both mutexes without deadlock
    std::lock(from.m, to.m);
    // make sure both already-locked mutexes are unlocked at the end of scope
    std::lock_guard<std::mutex> lock1(from.m, std::adopt_lock);
    std::lock_guard<std::mutex> lock2(to.m, std::adopt_lock);

// equivalent approach:
//    std::unique_lock<std::mutex> lock1(from.m, std::defer_lock);
//    std::unique_lock<std::mutex> lock2(to.m, std::defer_lock);
//    std::lock(lock1, lock2);

    from.balance -= amount;
    to.balance += amount;
}

int main()
{
    bank_account my_account(100);
    bank_account your_account(50);

    std::thread t1(transfer, std::ref(my_account), std::ref(your_account), 10);
    std::thread t2(transfer, std::ref(your_account), std::ref(my_account), 5);

    t1.join();
    t2.join();

    std::cout << "my_account: " << my_account.balance << std::endl;
    std::cout << "your_account: " << your_account.balance << std::endl;
}

// Output
// my_account: 95
// your_account: 55
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值