0

我有一个像这样的类层次结构(这是实际的类,但我清理了它):

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>为一个Notifiablefor 函数调用。你能证实吗?

更新对不起,我以为我可以避免将我的所有代码倾倒在问题中,但实际上我为Batch<Exp>::run(). 仍然缺少一些细节,但我并不认为它们是相关的(例如,我如何为实验生成参数)。

谢谢

4

2 回答 2

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

于 2012-10-16T09:21:41.740 回答
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根据你想要的来代替。)

于 2012-10-16T09:50:40.880 回答