我有一个像这样的类层次结构(这是实际的类,但我清理了它):
class Notifiable
{
public:
void notify();
}
template <class Exp>
class Batch : public Notifiable
{
public:
void run();
}
void Batch<Exp>::run()
{
done.clear();
generator->resetGeneration();
while(generator->hasMoreParameters())
{
// Lock for accessing active
std::unique_lock<std::mutex> lock(q_mutex, std::adopt_lock);
// If we've less experiments than threads
if (active.size() < threads)
{
Configuration conf = generator->generateParameters();
Exp e(executable, conf);
//std::weak_ptr<Batch<Exp>> bp;
//bp.reset(this);
std::thread t(&Exp::run, e, *this);
std::thread::id id = t.get_id();
active.insert(id);
t.detach();
}
q_control.wait(lock, [this] { return active.size() < threads; } );
}
}
class Experiment
{
public:
void run(Notifiable& caller)
{
do_stuff();
caller.notify();
}
virtual void do_stuff() = 0;
}
class MyExperiment : public Experiment
{
public:
void do_stuff()
{
// do my stuff
}
}
然后,我使用以下代码实例化Batch<MyExperiment>
对象并调用run()
:
Batch<ELExperiment> b(pex, options["name"].as<string>(), options["executable"].as<string>());
b.run();
但是我在编译时得到了这个:
In file included from /opt/local/include/gcc47/c++/bits/move.h:57:0,
from /opt/local/include/gcc47/c++/bits/stl_pair.h:61,
from /opt/local/include/gcc47/c++/bits/stl_algobase.h:65,
from /opt/local/include/gcc47/c++/bits/char_traits.h:41,
from /opt/local/include/gcc47/c++/ios:41,
from /opt/local/include/gcc47/c++/ostream:40,
from /opt/local/include/gcc47/c++/iostream:40,
from json2cli/main.cpp:9:
/opt/local/include/gcc47/c++/type_traits: In instantiation of 'struct std::_Result_of_impl<false, false, std::_Mem_fn<void (Experiment::*)(Notifiable&)>, MyExperiment, Batch<MyExperiment> >':
/opt/local/include/gcc47/c++/type_traits:1857:12: required from 'class std::result_of<std::_Mem_fn<void (Experiment::*)(Notifiable&)>(MyExperiment, Batch<MyExperiment>)>'
/opt/local/include/gcc47/c++/functional:1563:61: required from 'struct std::_Bind_simple<std::_Mem_fn<void (Experiment::*)(Notifiable&)>(MyExperiment, Batch<MyExperiment>)>'
/opt/local/include/gcc47/c++/thread:133:9: required from 'std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (Experiment::*)(Notifiable&); _Args = {MyExperiment&, Batch<MyExperiment>&}]'
json2cli/batch.hh:86:46: required from 'void Batch<Exp>::run() [with Exp = MyExperiment]'
json2cli/main.cpp:113:15: required from here
/opt/local/include/gcc47/c++/type_traits:1834:9: error: no match for call to '(std::_Mem_fn<void (Experiment::*)(Notifiable&)>) (MyExperiment, Batch<MyExperiment>)'
In file included from /opt/local/include/gcc47/c++/memory:81:0,
from json2cli/parameterexpression.hh:19,
from json2cli/main.cpp:13:
/opt/local/include/gcc47/c++/functional:525:11: note: candidates are:
/opt/local/include/gcc47/c++/functional:548:7: note: _Res std::_Mem_fn<_Res (_Class::*)(_ArgTypes ...)>::operator()(_Class&, _ArgTypes ...) const [with _Res = void; _Class = Experiment; _ArgTypes = {Notifiable&}]
/opt/local/include/gcc47/c++/functional:548:7: note: no known conversion for argument 1 from 'MyExperiment' to 'Experiment&'
/opt/local/include/gcc47/c++/functional:553:7: note: _Res std::_Mem_fn<_Res (_Class::*)(_ArgTypes ...)>::operator()(_Class*, _ArgTypes ...) const [with _Res = void; _Class = Experiment; _ArgTypes = {Notifiable&}]
/opt/local/include/gcc47/c++/functional:553:7: note: no known conversion for argument 1 from 'MyExperiment' to 'Experiment*'
/opt/local/include/gcc47/c++/functional:559:2: note: template<class _Tp> _Res std::_Mem_fn<_Res (_Class::*)(_ArgTypes ...)>::operator()(_Tp&, _ArgTypes ...) const [with _Tp = _Tp; _Res = void; _Class = Experiment; _ArgTypes = {Notifiable&}]
/opt/local/include/gcc47/c++/functional:559:2: note: template argument deduction/substitution failed:
In file included from /opt/local/include/gcc47/c++/bits/move.h:57:0,
from /opt/local/include/gcc47/c++/bits/stl_pair.h:61,
from /opt/local/include/gcc47/c++/bits/stl_algobase.h:65,
from /opt/local/include/gcc47/c++/bits/char_traits.h:41,
from /opt/local/include/gcc47/c++/ios:41,
from /opt/local/include/gcc47/c++/ostream:40,
from /opt/local/include/gcc47/c++/iostream:40,
from json2cli/main.cpp:9:
/opt/local/include/gcc47/c++/type_traits:1834:9: note: cannot convert 'std::declval<Batch<MyExperiment> >()' (type 'Batch<MyExperiment>') to type 'Notifiable&'
看起来我不能期望将任何Batch<Exp>
概括为Notifiable
以进行函数调用。你能证实吗?
更新抱歉,我认为我可以避免将所有代码转储到问题中,但实际上我为Batch<Exp>::run()
生成线程的方式肯定有问题。仍然有一些细节缺失,但我并不认为它们是相关的(例如我如何为实验生成参数)。
由于
答案 0 :(得分:2)
您的错误不在您向我们展示的代码中,您尝试绑定run
的某些代码并使用std::thread
创建一个线程,这就是问题,因为它可以'为绑定函数创建一个正确的结构,最简单的解决方法是编写自己的包装器:
template< class Expr >
struct my_bind {
my_bind( Expr& e, Notifiable& n ) : e_( e ), n_(n) {}
void operator()() {e_.run(n_);}
Expr& e_;
Notifiable& n_;
};
然后使用你自己的包装器来启动这个功能,我不能肯定地说,但我认为这是编译器中的一个错误(所有编译器都有这个错误:GCC,MSVC,......)当你表达式得到很复杂,他们没有在std::bind
!!
答案 1 :(得分:1)
更改
std::thread t(&Exp::run, e, *this);
到
std::thread t([](Exp&& e, Batch& b) { e.run(b); }, std::move(e), std::ref(*this));
或者,如果您真的希望线程从*this
继承副本:
std::thread t([](Exp&& e, Batch&& b) { e.run(b); }, std::move(e), *this);
错误的根源(至少是消息引用的那个)是特定std::thread
构造函数的语义(我不会在这里公开,它有点像血腥)。如果您已经熟悉语义std::bind
(它们有自己的怪癖),您可以遵从它:
std::thread t(std::bind(&Exp::run, std::move(e), std::ref(*this));
(std::ref(*this)
可以替换为*this
,具体取决于您的需求。)