我正在使用boost::thread
从同一个类的不同成员函数中调用类内成员函数。我想要解决的方法的声明是:
void ClassName::nnMetropolisStep(double random, std::vector<int> nums);
我正在通过以下方式从另一个成员函数创建线程:
boost::thread* tThread = new boost::thread(&ClassName::nnMetropolisStep,
this,
someRandom,someNums);
这些是我正在使用的代码中boost
函数的唯一调用。
我在其他问题中已经看到这种语法适用于非静态成员函数(并且我构造线程的方式没有访问问题)。但是,当我编译时,我收到以下错误:
g++ -fPIC -std=c++11 -c -g -Wall `root-config --cflags --glibs` -MMD -c -o obj/IsingModel.o src/IsingModel.cpp
In file included from /usr/include/boost/thread/pthread/mutex.hpp:11:0,
from /usr/include/boost/thread/mutex.hpp:16,
from /usr/include/boost/thread/pthread/thread_data.hpp:12,
from /usr/include/boost/thread/thread.hpp:17,
from /usr/include/boost/thread.hpp:13,
from src/interface/IsingModel.h:11,
from src/IsingModel.cpp:11:
/usr/include/boost/thread/locks.hpp: In instantiation of 'boost::unique_lock<Mutex>& boost::unique_lock<Mutex>::operator=(boost::unique_lock<Mutex>&&) [with Mutex = boost::mutex]':
/usr/include/boost/thread/future.hpp:414:33: required from here
/usr/include/boost/thread/locks.hpp:269:22: error: cannot bind 'boost::unique_lock<boost::mutex>' lvalue to 'boost::unique_lock<boost::mutex>&&'
swap(temp);
^
/usr/include/boost/thread/locks.hpp:279:14: note: initializing argument 1 of 'void boost::unique_lock<Mutex>::swap(boost::unique_lock<Mutex>&&) [with Mutex = boost::mutex]'
void swap(unique_lock&& other)
^
make: *** [obj/IsingModel.o] Error 1
发生了什么事?很明显,我要么做错了,要么更糟糕的是,我的编译器设置存在问题。
答案 0 :(得分:1)
我通过删除boost
除#include
语句之外的所有引用来找出答案。在我的头文件中,我正在使用
#include "boost/thread.hpp"
哪个有效,但不正确。一旦我将其改为
#include "boost/thread/thread.hpp"
所有编辑都没有投诉。
我使用的是版本1.41.0
。
答案 1 :(得分:-2)
您需要在创建线程对象期间使用boost::bind
函数。例如:
double rand = 0;
std::vector<int> myVector;
ClassName obj;
auto threadObj = new boost::thread(boost::bind(&ClassName::nnMetropolisStep,&obj,rand,myVector));