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>化することは期待できないようです。Notifiableそれを確認できますか?

更新申し訳ありませんが、質問内にすべてのコードをダンプすることを避けることができると思いましたが、実際には、スレッドを生成する方法に問題があるに違いありません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 に答える