c++11 mutex/atomic

mutex/atomic

c++ basic data struct is not thread-safe, only multithreading read and no write is safe. mutex/atomic tool can solve data race, but if you use them incorrect, it will cause deadlock.

mutex

basic mutex

basic mutex is std::mutex object, very simple rule: call member function lock to lock it, call unlock to unlock it. but you can not lock it when it locked, also not unlock it when it not locked, or it will cause deadlock.

std::mutex mtx;
mtx.lock();
// ...
mtx.unlock();

recursive mutex

recursive mutex is std::recursive_mutex object, difference from basic mutex, it will not cause deadlock when you locked it twice or more, but you need to unlock it the same times as you locked it, it is slower than basic mutex.

std::recursive_mutex rmtx;
rmtx.lock();
// ...
rmtx.unlock();

guard lock

guard lock is std::guard_lock<T> template, use feature RAII to lock/unlock mutex when object constructs and destructs. you just need a mutex object for its construct, you can not call lock/unlock for its mutex object manually.

std::mutex mtx;
std::guard_lock<std::mutex> lg(mtx);
// ...

unique lock

unique lock is std::unique_lock<T> template, very similar with guard lock, but you can call lock/unlock for its mutex object manually, so it is always used with condition variable, it is slow than guard lock.

std::mutex mtx;
std::unique_lock<std::mutex> lg(mtx);
// ...
mtx.unlock()
// ...
mtx.lock();

atomic

once atomic

sometimes you need a code part just run once, but many threads want to do that. you can use std::once_flag variable and std::call_once function to avoid data race.

std::once_flag of;
void once_func()
{
    // init...
}
// ...
std::call_once(of, once_func);

regular atomic

atomic is std::atomic<T> template, T must be c++ basic type, such as int, double and so on, use this template, this variable will be atomic, most of its operation is the same to its basic type but slow a little bit.

std::atomic<int> cnt;
// ...
++cnt;

memory order

atomic has six memory order, default use std::memory_order_seq_cst, unless very need perfermance, do not use other memory orders, its very easy to cause problem, here gives a spin lock class implements.

class spin_lock {
public:
    spin_lock() {
        while (flag.test_and_set(std::memory_order_acquire)) {}
    }
    ~spin_lock() {
        flag.clear(std::memory_order_release);
    }
private:
    std::atomic_flag flag;
};

转载于:https://my.oschina.net/u/4078154/blog/3089656

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值