C++ 有没有在boost 1.48.0下使用互斥的最新示例?

C++ 有没有在boost 1.48.0下使用互斥的最新示例?,c++,multithreading,boost,mutex,C++,Multithreading,Boost,Mutex,我在web上找到的大多数示例都已经过时,使用boost::mutex,但我没有声明包含或。在1.48.0版中有没有关于如何使用boost::mutex的明确示例?非常不清楚,不提供任何代码示例。检查此示例(boost::mutex用法在Resource::use()中介绍): #包括 #包括 类资源 { 公众: 资源():i(0){} 无效使用() { boost::mutex::作用域锁定(guard); ++一,; } 私人: int i; 互斥保护; }; 无效线程功能(资源和资源) {

我在web上找到的大多数示例都已经过时,使用boost::mutex,但我没有声明包含或。在1.48.0版中有没有关于如何使用boost::mutex的明确示例?非常不清楚,不提供任何代码示例。

检查此示例(
boost::mutex
用法在
Resource::use()
中介绍):

#包括
#包括
类资源
{
公众:
资源():i(0){}
无效使用()
{
boost::mutex::作用域锁定(guard);
++一,;
}
私人:
int i;
互斥保护;
};
无效线程功能(资源和资源)
{
resource.use();
}
int main()
{
资源;
boost::线程组线程组;
创建线程(boost::bind(thread\u func,boost::ref(resource));
创建线程(boost::bind(thread\u func,boost::ref(resource));
线程组。连接所有线程();
返回0;
}

我不记得从boost 1.34更新到更新版本时boost::mutex有任何问题。你能告诉我们更多关于实际问题的细节吗?@BenC:事实上,我现在发现这个问题很奇怪。使用boost::mutex可以成功编译,除了Eclipse中有一个错误提示“boost::mutex无法解析”,但是当我尝试实际构建项目时,它工作得很顺利。不知道为什么会这样。我几年前就停止使用Eclipse了,所以在这方面我真的帮不了你。有时IDE提供的信息可能会产生误导,但最重要的是编译不会出错。但是,如果您在编译过程中偶然发现与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;
}