多线程之互斥锁(mutex)的使用方法 | 您所在的位置:网站首页 › u型锁的使用方法 › 多线程之互斥锁(mutex)的使用方法 |
关于读写锁可查看:多线程之读写锁(unique_lock与shared_lock) 多个线程访问同一资源时,为了保证数据的一致性,最简单的方式就是使用 mutex(互斥锁)。 引用 cppreference 的介绍: The mutex class is a synchronization primitive that can be used to protect shared data from being simultaneously accessed by multiple threads.方法1:直接操作 mutex,即直接调用 mutex 的 lock / unlock 函数 此例顺带使用了 boost::thread_group 来创建一组线程。 #include #include #include boost::mutex mutex; int count = 0; void Counter() { mutex.lock(); int i = ++count; std::cout |
CopyRight 2018-2019 实验室设备网 版权所有 |