加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 在boost 1.48.0下是否有使用互斥锁的最新示例?

发布时间:2020-12-16 05:34:08 所属栏目:百科 来源:网络整理
导读:我在网上找到的大多数例子都是过时的,使用了boost :: mutex,我没有宣布包括或者.有没有明确的例子说明如何在ver 1.48.0中使用boost :: mutex? The tutorials in Chapter 27 (Threads)非常不清楚,不提供任何代码示例. 解决方法 检查此示例(在:: Resource()中
我在网上找到的大多数例子都是过时的,使用了boost :: mutex,我没有宣布包括或者.有没有明确的例子说明如何在ver 1.48.0中使用boost :: mutex? The tutorials in Chapter 27 (Threads)非常不清楚,不提供任何代码示例.

解决方法

检查此示例(在:: Resource()中显示了boost :: mutex用法):
#include <boost/thread.hpp>
#include <boost/bind.hpp>

class Resource
{
public:
    Resource(): i(0) {}

    void use()
    {
        boost::mutex::scoped_lock lock(guard);
        ++i;
    }

private:
    int i;
    boost::mutex guard;
};

void thread_func(Resource& resource)
{
    resource.use();
}

int main()
{
    Resource resource;
    boost::thread_group thread_group;
    thread_group.create_thread(boost::bind(thread_func,boost::ref(resource)));
    thread_group.create_thread(boost::bind(thread_func,boost::ref(resource)));
    thread_group.join_all();
    return 0;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读