我在网上找到的大多数例子都是过时的,使用了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;
}

