我在网上找到的大多数示例都已过时,使用的是 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/