Multithreading 在std::线程(非std::async)的std::packaged_任务中使用成员函数

Multithreading 在std::线程(非std::async)的std::packaged_任务中使用成员函数,multithreading,c++11,stl,packaged-task,Multithreading,C++11,Stl,Packaged Task,我试图从std::thread获得std::future,似乎唯一的方法(?)是使用std::packaged_任务。另外,我想在std::thread中调用的函数是一个类成员函数。我可以找到这个线程,但是他们使用std::async而不是std::thread。我试着根据那篇文章编译一些东西,但当我试着在这里编译这段代码时: #include <iostream> #include <future> #include <thread> class test

我试图从std::thread获得std::future,似乎唯一的方法(?)是使用std::packaged_任务。另外,我想在std::thread中调用的函数是一个类成员函数。我可以找到这个线程,但是他们使用std::async而不是std::thread。我试着根据那篇文章编译一些东西,但当我试着在这里编译这段代码时:

#include <iostream>
#include <future>
#include <thread>

class test
{
    int fthread(int b)
    {
        return a + b;
    }

    int a;
public:
    test(int a_) : a(a_) {}

    int run(int b)
    {
        std::packaged_task<int(int)>
            task(std::bind(&test::fthread, this));

        //std::thread a(std::move(std::ref(task)), 5, 1);
        std::thread th(std::move(std::ref(task)), b);//<--Edit typo...

        std::future<int> fut = task.get_future();

        return fut.get();
    }
};

int main()
{
    int a = 5, b = 1;
    test t(a);

    int c = t.run(b);

    if (c != (a+b))
        return 1;
    return 0;
}
#包括
#包括
#包括
课堂测试
{
intfthread(intb)
{
返回a+b;
}
INTA;
公众:
测试(inta_uu):a(a_u){
int运行(int b)
{
打包任务
任务(std::bind(&test::fthread,this));
//std::线程a(std::move(std::ref(task)),5,1);
std::thread th(std::move(std::ref(task)),b);/C:\MesProgs\Microsoft visualstudio 12.0\VC\include\functional(1137):请参阅正在编译的函数模板实例化“int std::_Bind::_Do_call(std::tuple,std:_Arg_idx)”
1> 与
1>          [
1> _Ty=int
1>          ]
1> C:\MesProgs\Microsoft Visual Studio 12.0\VC\include\functional(1137):请参阅正在编译的函数模板实例化“int std::_Bind::_Do_call(std::tuple,std:_Arg_idx)”的参考
1> 与
1>          [
1> _Ty=int
1>          ]
1> C:\MesProgs\Microsoft Visual Studio 12.0\VC\include\xrefwrap(283):请参阅正在编译的函数模板实例化“int std::_Bind::operator()(_Ty&&)”
1> 与
1>          [
1> _Ty=int
1>          ]
1> C:\MesProgs\Microsoft Visual Studio 12.0\VC\include\xrefwrap(283):请参阅正在编译的函数模板实例化“int std::_Bind::operator()(_Ty&&)”
1> 与
1>          [
1> _Ty=int
1>          ]
1> C:\MesProgs\Microsoft Visual Studio 12.0\VC\include\functional(228):请参阅正在编译的函数模板实例化参考“\u Ret std::\u Callable\u obj::\u ApplyX(\u Ty&&)”
1> 与
1>          [
1> _Ret=int
1> ,_Rx=int
1> ,_Ty=int
1>          ]
1> C:\MesProgs\Microsoft Visual Studio 12.0\VC\include\functional(228):请参阅正在编译的函数模板实例化参考“\u Ret std::\u Callable\u obj::\u ApplyX(\u Ty&&)”
1> 与
1>          [
1> _Ret=int
1> ,_Rx=int
1> ,_Ty=int
1>          ]
1> C:\MesProgs\Microsoft Visual Studio 12.0\VC\include\functional(226):编译类模板成员函数“int std::_Func\u impl::_Do_call(int&)”时
1> 与
1>          [
1> _Alloc=std::分配器
1> ,_Ret=int
1>          ]
1> C:\MesProgs\Microsoft Visual Studio 12.0\VC\include\functional(495):请参阅正在编译的类模板实例化“std::_Func_impl”的参考
1> 与
1>          [
1> _Alloc=std::分配器
1> ,_Ret=int
1>          ]
1> C:\MesProgs\Microsoft Visual Studio 12.0\VC\include\functional(396):请参阅正在编译的函数模板实例化“void std::_Func_class::_Do_alloc(_Fty&&,_alloc)”的参考
1> 与
1>          [
1> _Ret=int
1> ,_Ty=std::_Bind
1> ,_Alloc=std::分配器
1> ,_Fty=std::_Bind
1>          ]
1> C:\MesProgs\Microsoft Visual Studio 12.0\VC\include\functional(396):请参阅正在编译的函数模板实例化“void std::_Func_class::_Do_alloc(_Fty&&,_alloc)”的参考
1> 与
1>          [
1> _Ret=int
1> ,_Ty=std::_Bind
1> ,_Alloc=std::分配器
1> ,_Fty=std::_Bind
1>          ]
1> C:\MesProgs\Microsoft Visual Studio 12.0\VC\include\functional(385):请参阅正在编译的函数模板实例化“void std::_Func_class::_Reset_alloc(_Fty&&,_alloc)”的参考
1> 与
1>          [
1> _Ret=int
1> ,_Ty=std::_Bind
1> ,_Fty=std::_Bind
1> ,_Alloc=std::分配器
1>          ]
1> C:\MesProgs\Microsoft Visual Studio 12.0\VC\include\functional(385):请参阅正在编译的函数模板实例化“void std::_Func_class::_Reset_alloc(_Fty&&,_alloc)”的参考
1> 与
1>          [
1> _Ret=int
1> ,_Ty=std::_Bind
1> ,_Fty=std::_Bind
1> ,_Alloc=std::分配器
1>          ]
1> C:\MesProgs\Microsoft Visual Studio 12.0\VC\include\functional(671):请参阅正在编译的函数模板实例化“void std::_Func_class::_Reset(_Fty&&)”的参考
1> 与
1>          [
1> _Ret=int
1> ,_Ty=std::_Bind
1> ,_Fty=std::_Bind
1>          ]
1> C:\MesProgs\Microsoft Visual Studio 12.0\VC\include\functional(671):请参阅正在编译的函数模板实例化“void std::_Func_class::_Reset(_Fty&&)”的参考
1> 与
1>          [
1> _Ret=int
1> ,_Ty=std::_Bind
1> ,_Fty=std::_Bind
1>          ]
1> C:\MesProgs\Microsoft Visual Studio 12.0\VC\include\future(588):请参阅正在编译的函数模板实例化“std::function::function(_Fx&&)”的参考
1> 与
1>          [
1> _Ret=int
1> ,_Ty=std::_Bind
1> ,_Fx=std::_Bind
1>          ]
1> C:\MesProgs\Microsoft Visual Studio 12.0\VC\include\future(588):请参阅正在编译的函数模板实例化“std::function::function(_Fx&&)”的参考
1> 与
1>          [
1> _Ret=int
1> ,_Ty=std::_Bind
1> ,_Fx=std::_Bind
1>          ]
1> C:\MesProgs\Microsoft Visual Studio 12.0\VC\include\future(1742):请参阅正在编译的函数模板实例化“std::_packated_state::_packated_state(_Fty2&&)”
1> wi
error C2064: term does not evaluate to a function taking 1 arguments
class does not define an 'operator()' or a user defined conversion operator to a pointer-to-function or reference-to-function that takes appropriate number of arguments

1>          C:\MesProgs\Microsoft Visual Studio 12.0\VC\include\functional(1137) : see reference to function template instantiation 'int std::_Bind<true,int,std::_Pmf_wrap<int (__cdecl test::* )(int),int,test,int>,test *const >::_Do_call<_Ty,0x00>(std::tuple<_Ty &>,std::_Arg_idx<0x00>)' being compiled
1>          with
1>          [
1>              _Ty=int
1>          ]
1>          C:\MesProgs\Microsoft Visual Studio 12.0\VC\include\functional(1137) : see reference to function template instantiation 'int std::_Bind<true,int,std::_Pmf_wrap<int (__cdecl test::* )(int),int,test,int>,test *const >::_Do_call<_Ty,0x00>(std::tuple<_Ty &>,std::_Arg_idx<0x00>)' being compiled
1>          with
1>          [
1>              _Ty=int
1>          ]
1>          C:\MesProgs\Microsoft Visual Studio 12.0\VC\include\xrefwrap(283) : see reference to function template instantiation 'int std::_Bind<true,int,std::_Pmf_wrap<int (__cdecl test::* )(int),int,test,int>,test *const >::operator ()<_Ty>(_Ty &&)' being compiled
1>          with
1>          [
1>              _Ty=int
1>          ]
1>          C:\MesProgs\Microsoft Visual Studio 12.0\VC\include\xrefwrap(283) : see reference to function template instantiation 'int std::_Bind<true,int,std::_Pmf_wrap<int (__cdecl test::* )(int),int,test,int>,test *const >::operator ()<_Ty>(_Ty &&)' being compiled
1>          with
1>          [
1>              _Ty=int
1>          ]
1>          C:\MesProgs\Microsoft Visual Studio 12.0\VC\include\functional(228) : see reference to function template instantiation '_Ret std::_Callable_obj<std::_Bind<true,_Ret,std::_Pmf_wrap<int (__cdecl test::* )(int),int,test,int>,test *const >,false>::_ApplyX<_Rx,_Ty>(_Ty &&)' being compiled
1>          with
1>          [
1>              _Ret=int
1>  ,            _Rx=int
1>  ,            _Ty=int
1>          ]
1>          C:\MesProgs\Microsoft Visual Studio 12.0\VC\include\functional(228) : see reference to function template instantiation '_Ret std::_Callable_obj<std::_Bind<true,_Ret,std::_Pmf_wrap<int (__cdecl test::* )(int),int,test,int>,test *const >,false>::_ApplyX<_Rx,_Ty>(_Ty &&)' being compiled
1>          with
1>          [
1>              _Ret=int
1>  ,            _Rx=int
1>  ,            _Ty=int
1>          ]
1>          C:\MesProgs\Microsoft Visual Studio 12.0\VC\include\functional(226) : while compiling class template member function 'int std::_Func_impl<_MyWrapper,_Alloc,_Ret,int>::_Do_call(int &&)'
1>          with
1>          [
1>              _Alloc=std::allocator<std::_Func_class<int,int>>
1>  ,            _Ret=int
1>          ]
1>          C:\MesProgs\Microsoft Visual Studio 12.0\VC\include\functional(495) : see reference to class template instantiation 'std::_Func_impl<_MyWrapper,_Alloc,_Ret,int>' being compiled
1>          with
1>          [
1>              _Alloc=std::allocator<std::_Func_class<int,int>>
1>  ,            _Ret=int
1>          ]
1>          C:\MesProgs\Microsoft Visual Studio 12.0\VC\include\functional(396) : see reference to function template instantiation 'void std::_Func_class<_Ret,int>::_Do_alloc<_Myimpl,_Ty,_Alloc>(_Fty &&,_Alloc)' being compiled
1>          with
1>          [
1>              _Ret=int
1>  ,            _Ty=std::_Bind<true,int,std::_Pmf_wrap<int (__cdecl test::* )(int),int,test,int>,test *const >
1>  ,            _Alloc=std::allocator<std::_Func_class<int,int>>
1>  ,            _Fty=std::_Bind<true,int,std::_Pmf_wrap<int (__cdecl test::* )(int),int,test,int>,test *const >
1>          ]
1>          C:\MesProgs\Microsoft Visual Studio 12.0\VC\include\functional(396) : see reference to function template instantiation 'void std::_Func_class<_Ret,int>::_Do_alloc<_Myimpl,_Ty,_Alloc>(_Fty &&,_Alloc)' being compiled
1>          with
1>          [
1>              _Ret=int
1>  ,            _Ty=std::_Bind<true,int,std::_Pmf_wrap<int (__cdecl test::* )(int),int,test,int>,test *const >
1>  ,            _Alloc=std::allocator<std::_Func_class<int,int>>
1>  ,            _Fty=std::_Bind<true,int,std::_Pmf_wrap<int (__cdecl test::* )(int),int,test,int>,test *const >
1>          ]
1>          C:\MesProgs\Microsoft Visual Studio 12.0\VC\include\functional(385) : see reference to function template instantiation 'void std::_Func_class<_Ret,int>::_Reset_alloc<_Ty,std::allocator<std::_Func_class<_Ret,int>>>(_Fty &&,_Alloc)' being compiled
1>          with
1>          [
1>              _Ret=int
1>  ,            _Ty=std::_Bind<true,int,std::_Pmf_wrap<int (__cdecl test::* )(int),int,test,int>,test *const >
1>  ,            _Fty=std::_Bind<true,int,std::_Pmf_wrap<int (__cdecl test::* )(int),int,test,int>,test *const >
1>  ,            _Alloc=std::allocator<std::_Func_class<int,int>>
1>          ]
1>          C:\MesProgs\Microsoft Visual Studio 12.0\VC\include\functional(385) : see reference to function template instantiation 'void std::_Func_class<_Ret,int>::_Reset_alloc<_Ty,std::allocator<std::_Func_class<_Ret,int>>>(_Fty &&,_Alloc)' being compiled
1>          with
1>          [
1>              _Ret=int
1>  ,            _Ty=std::_Bind<true,int,std::_Pmf_wrap<int (__cdecl test::* )(int),int,test,int>,test *const >
1>  ,            _Fty=std::_Bind<true,int,std::_Pmf_wrap<int (__cdecl test::* )(int),int,test,int>,test *const >
1>  ,            _Alloc=std::allocator<std::_Func_class<int,int>>
1>          ]
1>          C:\MesProgs\Microsoft Visual Studio 12.0\VC\include\functional(671) : see reference to function template instantiation 'void std::_Func_class<_Ret,int>::_Reset<_Ty>(_Fty &&)' being compiled
1>          with
1>          [
1>              _Ret=int
1>  ,            _Ty=std::_Bind<true,int,std::_Pmf_wrap<int (__cdecl test::* )(int),int,test,int>,test *const >
1>  ,            _Fty=std::_Bind<true,int,std::_Pmf_wrap<int (__cdecl test::* )(int),int,test,int>,test *const >
1>          ]
1>          C:\MesProgs\Microsoft Visual Studio 12.0\VC\include\functional(671) : see reference to function template instantiation 'void std::_Func_class<_Ret,int>::_Reset<_Ty>(_Fty &&)' being compiled
1>          with
1>          [
1>              _Ret=int
1>  ,            _Ty=std::_Bind<true,int,std::_Pmf_wrap<int (__cdecl test::* )(int),int,test,int>,test *const >
1>  ,            _Fty=std::_Bind<true,int,std::_Pmf_wrap<int (__cdecl test::* )(int),int,test,int>,test *const >
1>          ]
1>          C:\MesProgs\Microsoft Visual Studio 12.0\VC\include\future(588) : see reference to function template instantiation 'std::function<_Ret (int)>::function<_Ty>(_Fx &&)' being compiled
1>          with
1>          [
1>              _Ret=int
1>  ,            _Ty=std::_Bind<true,int,std::_Pmf_wrap<int (__cdecl test::* )(int),int,test,int>,test *const >
1>  ,            _Fx=std::_Bind<true,int,std::_Pmf_wrap<int (__cdecl test::* )(int),int,test,int>,test *const >
1>          ]
1>          C:\MesProgs\Microsoft Visual Studio 12.0\VC\include\future(588) : see reference to function template instantiation 'std::function<_Ret (int)>::function<_Ty>(_Fx &&)' being compiled
1>          with
1>          [
1>              _Ret=int
1>  ,            _Ty=std::_Bind<true,int,std::_Pmf_wrap<int (__cdecl test::* )(int),int,test,int>,test *const >
1>  ,            _Fx=std::_Bind<true,int,std::_Pmf_wrap<int (__cdecl test::* )(int),int,test,int>,test *const >
1>          ]
1>          C:\MesProgs\Microsoft Visual Studio 12.0\VC\include\future(1742) : see reference to function template instantiation 'std::_Packaged_state<_Ret (int)>::_Packaged_state<_Ty>(_Fty2 &&)' being compiled
1>          with
1>          [
1>              _Ret=int
1>  ,            _Ty=std::_Bind<true,int,std::_Pmf_wrap<int (__cdecl test::* )(int),int,test,int>,test *const >
1>  ,            _Fty2=std::_Bind<true,int,std::_Pmf_wrap<int (__cdecl test::* )(int),int,test,int>,test *const >
1>          ]
1>          C:\MesProgs\Microsoft Visual Studio 12.0\VC\include\future(1744) : see reference to function template instantiation 'std::_Packaged_state<_Ret (int)>::_Packaged_state<_Ty>(_Fty2 &&)' being compiled
1>          with
1>          [
1>              _Ret=int
1>  ,            _Ty=std::_Bind<true,int,std::_Pmf_wrap<int (__cdecl test::* )(int),int,test,int>,test *const >
1>  ,            _Fty2=std::_Bind<true,int,std::_Pmf_wrap<int (__cdecl test::* )(int),int,test,int>,test *const >
1>          ]
1>          main.cpp(51) : see reference to function template instantiation 'std::packaged_task<int (int)>::packaged_task<std::_Bind<true,int,std::_Pmf_wrap<int (__cdecl test::* )(int),int,test,int>,test *const >>(_Fty2 &&)' being compiled
1>          with
1>          [
1>              _Fty2=std::_Bind<true,int,std::_Pmf_wrap<int (__cdecl test::* )(int),int,test,int>,test *const >
1>          ]
1>          main.cpp(51) : see reference to function template instantiation 'std::packaged_task<int (int)>::packaged_task<std::_Bind<true,int,std::_Pmf_wrap<int (__cdecl test::* )(int),int,test,int>,test *const >>(_Fty2 &&)' being compiled
1>          with
1>          [
1>              _Fty2=std::_Bind<true,int,std::_Pmf_wrap<int (__cdecl test::* )(int),int,test,int>,test *const >
1>          ]
int run(int b)
{
    std::packaged_task<int(int)>
            task(std::bind(&test::fthread, this, std::placeholders::_1));
    std::future<int> fut = task.get_future();

    std::thread(std::move(task), b).join();

    return fut.get();
}
int run(int b)
{
    return std::async(std::launch::async, [this,b]{ return fthread(b); }).get();
}