c++ - 有没有在boost 1.48.0下使用mutex的最新例子?

标签 c++ multithreading boost mutex

我在网上找到的大多数示例都已过时,使用的是 boost::mutex,我没有声明包括 或 .在 1.48.0 版中是否有任何明确的示例说明如何使用 boost::mutex? The tutorials in Chapter 27 (Threads)非常不清楚,不提供任何代码示例。

最佳答案

查看此示例(boost::mutex 用法在 Resource::use() 中提供):

#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;
}

关于c++ - 有没有在boost 1.48.0下使用mutex的最新例子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8175459/

相关文章:

multithreading - Prange 减慢 Cython 循环速度

c++ - 修改具有 hashed_non_unique 键的 Boost Multi-Index Map 中的键范围

c++ - Boost C++ 序列化开销

c++ - 在 C++ 代码中重新启动连接时出现“断管”错误

c++ - set<pair<K, V>> 默认有更大的仿函数

java - HoughLinesP 生成空图像

c - Glib/Gio 异步或线程 UDP 服务器

c++ - 异常后重试局部静态变量初始化

java - Android后台服务定时器无法正常工作

c++ - Win 事件替换为 boost 信号(线程安全)