如何将带有参数的方法传递给线程以执行?
How to pass method with arguments to thread to execution?
我有一个方法
void RobocopyCopy::monitoring_thread_method(std::string path_to_monior)
{
while (!m_bMonitorThreadShouldExit)
{
printf("HERE WE ARE!!!! Path is :: %s\n", path_to_monior.c_str());
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
}
我需要在后台启动它,所以我创建了一个线程并想将此方法作为参数传递
void RobocopyCopy::start_monitoring_thread(std::string path_to_monior)
{
m_bMonitorThreadShouldExit = false;
m_pMonitoringThread = std::unique_ptr<std::thread>(new std::thread(&RobocopyCopy::monitoring_thread_method, path_to_monior, this));
}
我遇到这样的错误
>RobocopyCopy.cpp
1>c:\program files (x86)\microsoft visual studio17\community\vc\tools\msvc.16.27023\include\thr\xthread(238): error C2672: 'std::invoke': no matching overloaded function found
1>c:\program files (x86)\microsoft visual studio17\community\vc\tools\msvc.16.27023\include\thr\xthread(246): note: see reference to function template instantiation 'void std::_LaunchPad<_Target>::_Execute<0,1,2>(std::tuple<void (__cdecl RobocopyCopy::* )(std::string),std::basic_string<char,std::char_traits<char>,std::allocator<char>>,RobocopyCopy *> &,std::integer_sequence<_Ty,0,1,2>)' being compiled
1> with
1> [
1> _Target=std::unique_ptr<std::tuple<void (__cdecl RobocopyCopy::* )(std::string),std::basic_string<char,std::char_traits<char>,std::allocator<char>>,RobocopyCopy *>,std::default_delete<std::tuple<void (__cdecl RobocopyCopy::* )(std::string),std::basic_string<char,std::char_traits<char>,std::allocator<char>>,RobocopyCopy *>>>,
1> _Ty=size_t
1> ]
1>c:\program files (x86)\microsoft visual studio17\community\vc\tools\msvc.16.27023\include\thr\xthread(245): note: see reference to function template instantiation 'void std::_LaunchPad<_Target>::_Execute<0,1,2>(std::tuple<void (__cdecl RobocopyCopy::* )(std::string),std::basic_string<char,std::char_traits<char>,std::allocator<char>>,RobocopyCopy *> &,std::integer_sequence<_Ty,0,1,2>)' being compiled
1> with
1> [
1> _Target=std::unique_ptr<std::tuple<void (__cdecl RobocopyCopy::* )(std::string),std::basic_string<char,std::char_traits<char>,std::allocator<char>>,RobocopyCopy *>,std::default_delete<std::tuple<void (__cdecl RobocopyCopy::* )(std::string),std::basic_string<char,std::char_traits<char>,std::allocator<char>>,RobocopyCopy *>>>,
1> _Ty=size_t
1> ]
1>c:\program files (x86)\microsoft visual studio17\community\vc\tools\msvc.16.27023\include\thr\xthread(242): note: while compiling class template member function 'void std::_LaunchPad<_Target>::_Run(std::_LaunchPad<_Target> *) noexcept'
1> with
1> [
1> _Target=std::unique_ptr<std::tuple<void (__cdecl RobocopyCopy::* )(std::string),std::basic_string<char,std::char_traits<char>,std::allocator<char>>,RobocopyCopy *>,std::default_delete<std::tuple<void (__cdecl RobocopyCopy::* )(std::string),std::basic_string<char,std::char_traits<char>,std::allocator<char>>,RobocopyCopy *>>>
1> ]
1>c:\program files (x86)\microsoft visual studio17\community\vc\tools\msvc.16.27023\include\thr\xthread(230): note: see reference to function template instantiation 'void std::_LaunchPad<_Target>::_Run(std::_LaunchPad<_Target> *) noexcept' being compiled
1> with
1> [
1> _Target=std::unique_ptr<std::tuple<void (__cdecl RobocopyCopy::* )(std::string),std::basic_string<char,std::char_traits<char>,std::allocator<char>>,RobocopyCopy *>,std::default_delete<std::tuple<void (__cdecl RobocopyCopy::* )(std::string),std::basic_string<char,std::char_traits<char>,std::allocator<char>>,RobocopyCopy *>>>
1> ]
1>c:\program files (x86)\microsoft visual studio17\community\vc\tools\msvc.16.27023\include\thr\xthread(257): note: see reference to class template instantiation 'std::_LaunchPad<_Target>' being compiled
1> with
1> [
1> _Target=std::unique_ptr<std::tuple<void (__cdecl RobocopyCopy::* )(std::string),std::basic_string<char,std::char_traits<char>,std::allocator<char>>,RobocopyCopy *>,std::default_delete<std::tuple<void (__cdecl RobocopyCopy::* )(std::string),std::basic_string<char,std::char_traits<char>,std::allocator<char>>,RobocopyCopy *>>>
1> ]
1>c:\program files (x86)\microsoft visual studio17\community\vc\tools\msvc.16.27023\include\thread(49): note: see reference to function template instantiation 'void std::_Launch<std::unique_ptr<std::tuple<void (__cdecl RobocopyCopy::* )(std::string),std::basic_string<char,std::char_traits<char>,std::allocator<char>>,RobocopyCopy *>,std::default_delete<_Ty>>>(_Thrd_t *,_Target &&)' being compiled
1> with
1> [
1> _Ty=std::tuple<void (__cdecl RobocopyCopy::* )(std::string),std::basic_string<char,std::char_traits<char>,std::allocator<char>>,RobocopyCopy *>,
1> _Target=std::unique_ptr<std::tuple<void (__cdecl RobocopyCopy::* )(std::string),std::basic_string<char,std::char_traits<char>,std::allocator<char>>,RobocopyCopy *>,std::default_delete<std::tuple<void (__cdecl RobocopyCopy::* )(std::string),std::basic_string<char,std::char_traits<char>,std::allocator<char>>,RobocopyCopy *>>>
1> ]
1>c:\tetavi_main2\tv_apps\tv_world2\tv_logic_cpp\robocopycopy.cpp(73): note: see reference to function template instantiation 'std::thread::thread<void(__cdecl RobocopyCopy::* )(std::string),std::string&,RobocopyCopy*,void>(_Fn &&,std::string &,RobocopyCopy *&&)' being compiled
1> with
1> [
1> _Fn=void (__cdecl RobocopyCopy::* )(std::string)
1> ]
1>c:\program files (x86)\microsoft visual studio17\community\vc\tools\msvc.16.27023\include\thr\xthread(238): error C2893: Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...) noexcept(<expr>)'
1>c:\program files (x86)\microsoft visual studio17\community\vc\tools\msvc.16.27023\include\thr\xthread(238): note: With the following template arguments:
1>c:\program files (x86)\microsoft visual studio17\community\vc\tools\msvc.16.27023\include\thr\xthread(238): note: '_Callable=void (__cdecl RobocopyCopy::* )(std::string)'
1>c:\program files (x86)\microsoft visual studio17\community\vc\tools\msvc.16.27023\include\thr\xthread(238): note: '_Types={std::basic_string<char,std::char_traits<char>,std::allocator<char>>, RobocopyCopy *}'
1>Done building project "TV_Logic_CPP.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
我做错了什么?
m_pMonitoringThread = std::make_unique<std::thread>(&RobocopyCopy::monitoring_thread_method, this, path_to_monior);
我有一个方法
void RobocopyCopy::monitoring_thread_method(std::string path_to_monior)
{
while (!m_bMonitorThreadShouldExit)
{
printf("HERE WE ARE!!!! Path is :: %s\n", path_to_monior.c_str());
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
}
我需要在后台启动它,所以我创建了一个线程并想将此方法作为参数传递
void RobocopyCopy::start_monitoring_thread(std::string path_to_monior)
{
m_bMonitorThreadShouldExit = false;
m_pMonitoringThread = std::unique_ptr<std::thread>(new std::thread(&RobocopyCopy::monitoring_thread_method, path_to_monior, this));
}
我遇到这样的错误
>RobocopyCopy.cpp
1>c:\program files (x86)\microsoft visual studio17\community\vc\tools\msvc.16.27023\include\thr\xthread(238): error C2672: 'std::invoke': no matching overloaded function found
1>c:\program files (x86)\microsoft visual studio17\community\vc\tools\msvc.16.27023\include\thr\xthread(246): note: see reference to function template instantiation 'void std::_LaunchPad<_Target>::_Execute<0,1,2>(std::tuple<void (__cdecl RobocopyCopy::* )(std::string),std::basic_string<char,std::char_traits<char>,std::allocator<char>>,RobocopyCopy *> &,std::integer_sequence<_Ty,0,1,2>)' being compiled
1> with
1> [
1> _Target=std::unique_ptr<std::tuple<void (__cdecl RobocopyCopy::* )(std::string),std::basic_string<char,std::char_traits<char>,std::allocator<char>>,RobocopyCopy *>,std::default_delete<std::tuple<void (__cdecl RobocopyCopy::* )(std::string),std::basic_string<char,std::char_traits<char>,std::allocator<char>>,RobocopyCopy *>>>,
1> _Ty=size_t
1> ]
1>c:\program files (x86)\microsoft visual studio17\community\vc\tools\msvc.16.27023\include\thr\xthread(245): note: see reference to function template instantiation 'void std::_LaunchPad<_Target>::_Execute<0,1,2>(std::tuple<void (__cdecl RobocopyCopy::* )(std::string),std::basic_string<char,std::char_traits<char>,std::allocator<char>>,RobocopyCopy *> &,std::integer_sequence<_Ty,0,1,2>)' being compiled
1> with
1> [
1> _Target=std::unique_ptr<std::tuple<void (__cdecl RobocopyCopy::* )(std::string),std::basic_string<char,std::char_traits<char>,std::allocator<char>>,RobocopyCopy *>,std::default_delete<std::tuple<void (__cdecl RobocopyCopy::* )(std::string),std::basic_string<char,std::char_traits<char>,std::allocator<char>>,RobocopyCopy *>>>,
1> _Ty=size_t
1> ]
1>c:\program files (x86)\microsoft visual studio17\community\vc\tools\msvc.16.27023\include\thr\xthread(242): note: while compiling class template member function 'void std::_LaunchPad<_Target>::_Run(std::_LaunchPad<_Target> *) noexcept'
1> with
1> [
1> _Target=std::unique_ptr<std::tuple<void (__cdecl RobocopyCopy::* )(std::string),std::basic_string<char,std::char_traits<char>,std::allocator<char>>,RobocopyCopy *>,std::default_delete<std::tuple<void (__cdecl RobocopyCopy::* )(std::string),std::basic_string<char,std::char_traits<char>,std::allocator<char>>,RobocopyCopy *>>>
1> ]
1>c:\program files (x86)\microsoft visual studio17\community\vc\tools\msvc.16.27023\include\thr\xthread(230): note: see reference to function template instantiation 'void std::_LaunchPad<_Target>::_Run(std::_LaunchPad<_Target> *) noexcept' being compiled
1> with
1> [
1> _Target=std::unique_ptr<std::tuple<void (__cdecl RobocopyCopy::* )(std::string),std::basic_string<char,std::char_traits<char>,std::allocator<char>>,RobocopyCopy *>,std::default_delete<std::tuple<void (__cdecl RobocopyCopy::* )(std::string),std::basic_string<char,std::char_traits<char>,std::allocator<char>>,RobocopyCopy *>>>
1> ]
1>c:\program files (x86)\microsoft visual studio17\community\vc\tools\msvc.16.27023\include\thr\xthread(257): note: see reference to class template instantiation 'std::_LaunchPad<_Target>' being compiled
1> with
1> [
1> _Target=std::unique_ptr<std::tuple<void (__cdecl RobocopyCopy::* )(std::string),std::basic_string<char,std::char_traits<char>,std::allocator<char>>,RobocopyCopy *>,std::default_delete<std::tuple<void (__cdecl RobocopyCopy::* )(std::string),std::basic_string<char,std::char_traits<char>,std::allocator<char>>,RobocopyCopy *>>>
1> ]
1>c:\program files (x86)\microsoft visual studio17\community\vc\tools\msvc.16.27023\include\thread(49): note: see reference to function template instantiation 'void std::_Launch<std::unique_ptr<std::tuple<void (__cdecl RobocopyCopy::* )(std::string),std::basic_string<char,std::char_traits<char>,std::allocator<char>>,RobocopyCopy *>,std::default_delete<_Ty>>>(_Thrd_t *,_Target &&)' being compiled
1> with
1> [
1> _Ty=std::tuple<void (__cdecl RobocopyCopy::* )(std::string),std::basic_string<char,std::char_traits<char>,std::allocator<char>>,RobocopyCopy *>,
1> _Target=std::unique_ptr<std::tuple<void (__cdecl RobocopyCopy::* )(std::string),std::basic_string<char,std::char_traits<char>,std::allocator<char>>,RobocopyCopy *>,std::default_delete<std::tuple<void (__cdecl RobocopyCopy::* )(std::string),std::basic_string<char,std::char_traits<char>,std::allocator<char>>,RobocopyCopy *>>>
1> ]
1>c:\tetavi_main2\tv_apps\tv_world2\tv_logic_cpp\robocopycopy.cpp(73): note: see reference to function template instantiation 'std::thread::thread<void(__cdecl RobocopyCopy::* )(std::string),std::string&,RobocopyCopy*,void>(_Fn &&,std::string &,RobocopyCopy *&&)' being compiled
1> with
1> [
1> _Fn=void (__cdecl RobocopyCopy::* )(std::string)
1> ]
1>c:\program files (x86)\microsoft visual studio17\community\vc\tools\msvc.16.27023\include\thr\xthread(238): error C2893: Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...) noexcept(<expr>)'
1>c:\program files (x86)\microsoft visual studio17\community\vc\tools\msvc.16.27023\include\thr\xthread(238): note: With the following template arguments:
1>c:\program files (x86)\microsoft visual studio17\community\vc\tools\msvc.16.27023\include\thr\xthread(238): note: '_Callable=void (__cdecl RobocopyCopy::* )(std::string)'
1>c:\program files (x86)\microsoft visual studio17\community\vc\tools\msvc.16.27023\include\thr\xthread(238): note: '_Types={std::basic_string<char,std::char_traits<char>,std::allocator<char>>, RobocopyCopy *}'
1>Done building project "TV_Logic_CPP.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
我做错了什么?
m_pMonitoringThread = std::make_unique<std::thread>(&RobocopyCopy::monitoring_thread_method, this, path_to_monior);