在线程池

时间:2017-12-04 22:19:03

标签: multithreading c++11 pool

我尝试在C ++ 11中编译以下代码但没有成功。我的目标是使用lambda表达式而不是bind来填充任务队列。 请你帮助我好吗? 有没有创建悬挂参考?

#include <iostream>
#include <functional>
#include <queue>
using namespace std;
std::queue<std::function<void()>> funcs;
class A
{
public:
    void foo(int a )
    {
        cout << "Hello word" << endl;
    }
};
class ThePool
{
 public:
 template<typename _Callable, typename Object, typename... _Args>
 void QueueFunction(_Callable __f, Object  obj, _Args... __args)
 { 
       funcs.push([=]()
                {
                     (obj.*__f)((__args...));
                });          
    }
    void print(int i )
    {
       cout << "Hello Word"<< i <<endl;
    }
};
int main(int argc, char** argv) {
ThePool t;
A a ;
t.QueueFunction(&A::foo,a,5);
std::function<void()> func = funcs.back();
func();
return 0;
}

生成的错误如下

  

main.cpp:24:38:错误:   参数包未使用'...'扩展:                             (OBJ __ F。)((__ ARGS ...));                                         ^ main.cpp:24:38:注意:'__ args'main.cpp:24:44:错误:预期')'在'...'之前                             (OBJ __ F。)((__ ARGS ...));                                               ^ main.cpp:24:49:错误:预期')'之前';'代币                             (OBJ * __ F。)((__ ARGS ...));                                                    ^ main.cpp:在函数'int main(int,char **)'中:main.cpp:39:45:错误:转换   从'std :: function'到非标量类型   要求'std :: function'        std :: function func = funcs.back();                                                ^ main.cpp:实例化'struct ThePool :: QueueFunction(_Callable,Object,   _Args ...)[with _Callable = void(A :: )(int);对象= A; _Args = {int}] :: __ lambda0':main.cpp:22:12:从'void中需要   ThePool :: QueueFunction(_Callable,Object,_ Args ...)[with _Callable =   void(A :: )(int);对象= A; _Args = {int}]'main.cpp:38:32:
  从这里需要main.cpp:24:38:错误:使用无效字段   'ThePool :: QueueFunction(_Callable,Object,_ Args   ...):: __ lambda0 :: ____ ARGS'                             (OBJ * __ F。)((__ ARGS ...));

0 个答案:

没有答案