0
私は再利用、単純なスレッドプールにしようとしていますが、SOで述べた(関数内でメンバーを変更する必要があります)キュー内にテンプレート化クラスのメンバ関数であるタスクを提出する必要がtemplatisedメンバ関数でスレッドプールを提出する方法
template<class T>
class A
{
private:
int x ;
public:
void fun(std::vector<std::string> & items)
{
//do somehting with items.
x = 5; // modify the members.
}// please note that i need to modify members in this function in submitted thread.
};
ので、最後に、私は何かを必要とlike-
thread_pool tp;
// a member function of class obj A (a) submitted with vector<string> v.
tp.submit(&A<int>::fun, std::ref(a), v);
私が持っているクエリは、タスクキューの署名が上記のタスクを実行するように見えますか? このtemplatizedメンバ関数を実行するには、thread_poolクラスをどのように変更する必要がありますか?私のコードでsubmit関数を呼び出すにはどうすればよいですか?
私はここで同様の質問を見ましたが、それについてはまだ疑問に思っていました。 同じ例が本当に役に立ちます。
ありがとうございました。
thread_pool tp;
A<int> a;
td::vector<std::string> v;
tp.submit([&]() { a.fun(v); });
注:
'tp.submit([&] {a.fun(v);});' –
ありがとうございます。出来た。 – user888270