活动对象 - 从函数对象类创建线程
Active objects - creating a thread from a function object class
我想创建活动对象,在实例化时启动一个新线程(执行其函数应用程序运算符)。
template <typename R, typename... Ts> // VARIADIC TEMPLATE
class active_object{
protected:
std::thread* t;
R& result;
public:
active_object(R init, Ts... args) : t{nullptr}, result{init} {
start_thread(args...);
}
void start_thread(Ts... args){
t = new std::thread( *this, args...);
}
~active_object(){
if(t->joinable())
t->join();
delete t;
}
void set_result(R& result_){
result = result_;
}
// pure virtual function => makes this class abstract
virtual void operator()(Ts... args) = 0;
};
然后:
class myactiveobject : public active_object<double,int,int,bool> {
public:
myactiveobject(double init, int i1, int i2, bool b) : active_object{init, i1, i2, b} {}
void operator()(int x, int y, bool which){
double result = 0.1;
if(which)
result *= x;
else
result *= y;
set_result(result);
}
};
现在,我收到此错误:
$ g++ -std=c++11 -pthread main.cpp
In file included from /usr/include/c++/4.7/functional:56:0,
from /usr/include/c++/4.7/thread:39,
from multithreading.h:2,
from main.cpp:1:
/usr/include/c++/4.7/tuple: In instantiation of ‘struct std::_Head_base<0u, roby::multithreading::active_object<double, int, int, bool>, false>’:
/usr/include/c++/4.7/tuple:215:12: required from ‘struct std::_Tuple_impl<0u, roby::multithreading::active_object<double, int, int, bool>, int, int, bool>’
/usr/include/c++/4.7/tuple:374:11: required from ‘class std::tuple<roby::multithreading::active_object<double, int, int, bool>, int, int, bool>’
/usr/include/c++/4.7/functional:1601:39: required from ‘struct std::_Bind_simple<roby::multithreading::active_object<double, int, int, bool>(int, int, bool)>’
/usr/include/c++/4.7/thread:133:9: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = roby::multithreading::active_object<double, int, int, bool>&; _Args = {int&, int&, bool&}]’
multithreading.h:36:5: required from ‘void roby::multithreading::active_object<R, Ts>::start_thread(Ts ...) [with R = double; Ts = {int, int, bool}]’
multithreading.h:31:5: required from ‘roby::multithreading::active_object<R, Ts>::active_object(R, Ts ...) [with R = double; Ts = {int, int, bool}]’
main.cpp:11:85: required from here
/usr/include/c++/4.7/tuple:166:13: error: cannot declare field ‘std::_Head_base<0u, roby::multithreading::active_object<double, int, int, bool>, false>::_M_head_impl’ to be of abstract type ‘roby::multithreading::active_object<double, int, int, bool>’
In file included from main.cpp:1:0:
multithreading.h:23:9: note: because the following virtual functions are pure within ‘roby::multithreading::active_object<double, int, int, bool>’:
multithreading.h:51:17: note: void roby::multithreading::active_object<R, Ts>::operator()(Ts ...) [with R = double; Ts = {int, int, bool}]
当我在非纯虚函数active_object operator() 的情况下,它会编译但在 std::thread() 构造上给我一个分割错误。
std::thread( *this, args...)
会复制*this
。好吧,尝试。它不能这样做,因为active_object
是一个抽象类(如果可能的话,情况会更糟,因为active_object
违反了三法则)。
您需要使用std::thread(std::ref(*this), args...)
以便std::thread
对象存储引用,而不是副本。
相关文章:
- 如何为模板化对象创建模板向量?VS正在投掷C3203
- 具有包含其他对象的类的对象创建顺序
- 为什么我们再次从结构对象创建结构变量?
- 将对象创建为全局/静态对象会崩溃,而本地对象不会崩溃
- 如何创建一个对象创建函数,该函数将由与其关联的名称调用?
- 如何为自定义模板对象创建专门的函数模板
- 对象创建错误的C++矢量
- 如何为Python Swigged C++对象创建和分配回调函数
- 在 C++ 中为 C 样式对象创建一个透明包装类
- 此类模板的对象创建如何工作?
- C++ 中的对象创建类型有什么区别?
- 未知大小的数组作为类成员,用于在运行时(对象创建时间)创建数组的对象
- 如何仅通过父类对象限制对象创建
- 在正确性或良好的代码结构方面,这种动态对象创建看起来如何
- 无法将类对象创建为另一个类的成员
- 如何从现有基类对象创建派生类对象
- 在 DTor 之前删除的静态对象创建的线程?
- C++:定义多个构造函数时的对象创建/销毁序列
- 从对象创建矢量包装器,该对象只允许使用索引访问向量
- 是否可以为CPP中的对象创建一组指针