futures、promise、およびパッケージ化されたタスクに関するこの優れたチュートリアルに従って、自分のタスクを準備したいと思うようになりました。
#include <iostream>
#include <future>
using namespace std;
int ackermann(int m, int n) { // might take a while
if(m==0) return n+1;
if(n==0) return ackermann(m-1,1);
return ackermann(m-1, ackermann(m, n-1));
}
int main () {
packaged_task<int(int,int)> task1 { &ackermann, 3, 11 }; // <- error
auto f1 = task1.get_future();
thread th1 { move(task1) }; // call
cout << " ack(3,11):" << f1.get() << endl;
th1.join();
}
gcc-4.7.0 エラーメッセージを解読できる限り、引数が異なると予想されますか? しかし、どのように?エラーメッセージを短くしようとしました:
error: no matching function for call to
'std::packaged_task<int(int, int)>::packaged_task(<brace-enclosed initializer list>)'
note: candidates are:
std::packaged_task<_Res(_ArgTypes ...)>::---<_Res(_ArgTypes ...)>&&) ---
note: candidate expects 1 argument, 3 provided
...
note: cannot convert 'ackermann'
(type 'int (*)(int, int)') to type 'std::allocator_arg_t'
私のバリアントは、パラメーターをackermann
間違って提供する方法ですか? それとも間違ったテンプレートパラメータですか? 3,11
スレッドの作成にパラメータを与えませんよね?
失敗した他のバリアントを更新します。
packaged_task<int()> task1 ( []{return ackermann(3,11);} );
thread th1 { move(task1) };
packaged_task<int()> task1 ( bind(&ackermann,3,11) );
thread th1 { move(task1) };
packaged_task<int(int,int)> task1 ( &ackermann );
thread th1 { move(task1), 3,11 };
うーん...それは私ですか、それともbeta-gccですか?