c++ - 如何正确扩展 std::thread 以包装生成的线程

标签 c++ c++11 stdthread

我正在尝试创建一个简单的包装器,供我的应用程序在 std::thread 中创建线程时使用。 使用此应用程序线程包装器的唯一目的是确保在每个生成的线程上一致地调用某些代码。我认为这很简单,但是 std::thread 构造函数和参数传递相当复杂,我在这里遇到了非常神秘的构建错误。

这是我正在尝试做的一个简单示例:

#include <thread>

class AppThread : public std::thread
{
    template< class Function, class... Args > 
    static void wrap( Function&& f, Args&&... args )
    {
        //Some code
        f( std::forward<Args>( args )... );
    }

public:
    template< class Function, class... Args > 
    explicit AppThread( Function&& f, Args&&... args ) : std::thread( AppThread::wrap<Function,Args...>,
                                                                      std::forward<Function>( f ), std::forward<Args>( args )... )
    {}
};

void runA() {}
void runB( int x ) {}

main()
{
    AppThread thread1 = AppThread( runA );
    //AppThread thread2 = AppThread( runB, 5 );
}

我希望能够在使用 std:thread 的任何地方加入 AppThread,因此扩展和覆盖构造函数似乎是最好的方法。但是将这些参数传递给我的包装方法会导致这一系列错误 (gcc 7.2)

   In file included from thread_wrap.cpp:1:0:
gcc-7.2.0/include/c++/7.2.0/thread: In instantiation of ‘struct std::thread::_Invoker<std::tuple<void (*)(void (&)()), void (*)()> >’:
gcc-7.2.0/include/c++/7.2.0/thread:127:22:   required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(void (&)()); _Args = {void (&)()}]’
thread_wrap.cpp:15:130:   required from ‘AppThread::AppThread(Function&&, Args&& ...) [with Function = void (&)(); Args = {}]’
thread_wrap.cpp:24:41:   required from here
gcc-7.2.0/include/c++/7.2.0/thread:240:2: error: no matching function for call to ‘std::thread::_Invoker<std::tuple<void (*)(void (&)()), void (*)()> >::_M_invoke(std::thread::_Invoker<std::tuple<void (*)(void (&)()), void (*)()> >::_Indices)’
  operator()()
  ^~~~~~~~
gcc-7.2.0/include/c++/7.2.0/thread:231:4: note: candidate: template<long unsigned int ..._Ind> decltype (std::__invoke((_S_declval<_Ind>)()...)) std::thread::_Invoker<_Tuple>::_M_invoke(std::_Index_tuple<_Ind ...>) [with long unsigned int ..._Ind = {_Ind ...}; _Tuple = std::tuple<void (*)(void (&)()), void (*)()>]
    _M_invoke(_Index_tuple<_Ind...>)
    ^~~~~~~~~
gcc-7.2.0/include/c++/7.2.0/thread:231:4: note:   template argument deduction/substitution failed:
gcc-7.2.0/include/c++/7.2.0/thread: In substitution of ‘template<long unsigned int ..._Ind> decltype (std::__invoke(_S_declval<_Ind>()...)) std::thread::_Invoker<std::tuple<void (*)(void (&)()), void (*)()> >::_M_invoke<_Ind ...>(std::_Index_tuple<_Ind1 ...>) [with long unsigned int ..._Ind = {0, 1}]’:
gcc-7.2.0/include/c++/7.2.0/thread:240:2:   required from ‘struct std::thread::_Invoker<std::tuple<void (*)(void (&)()), void (*)()> >’
gcc-7.2.0/include/c++/7.2.0/thread:127:22:   required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(void (&)()); _Args = {void (&)()}]’
thread_wrap.cpp:15:130:   required from ‘AppThread::AppThread(Function&&, Args&& ...) [with Function = void (&)(); Args = {}]’
thread_wrap.cpp:24:41:   required from here
gcc-7.2.0/include/c++/7.2.0/thread:233:29: error: no matching function for call to ‘__invoke(std::__tuple_element_t<0, std::tuple<void (*)(void (&)()), void (*)()> >, std::__tuple_element_t<1, std::tuple<void (*)(void (&)()), void (*)()> >)’
    -> decltype(std::__invoke(_S_declval<_Ind>()...))
                ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
In file included from gcc-7.2.0/include/c++/7.2.0/tuple:41:0,
                 from gcc-7.2.0/include/c++/7.2.0/bits/unique_ptr.h:37,
                 from gcc-7.2.0/include/c++/7.2.0/memory:80,
                 from gcc-7.2.0/include/c++/7.2.0/thread:39,
                 from thread_wrap.cpp:1:
gcc-7.2.0/include/c++/7.2.0/bits/invoke.h:89:5: note: candidate: template<class _Callable, class ... _Args> constexpr typename std::__invoke_result<_Functor, _ArgTypes>::type std::__invoke(_Callable&&, _Args&& ...)
     __invoke(_Callable&& __fn, _Args&&... __args)
     ^~~~~~~~
gcc-7.2.0/include/c++/7.2.0/bits/invoke.h:89:5: note:   template argument deduction/substitution failed:
gcc-7.2.0/include/c++/7.2.0/bits/invoke.h: In substitution of ‘template<class _Callable, class ... _Args> constexpr typename std::__invoke_result<_Functor, _ArgTypes>::type std::__invoke(_Callable&&, _Args&& ...) [with _Callable = void (*)(void (&)()); _Args = {void (*)()}]’:
gcc-7.2.0/include/c++/7.2.0/thread:233:29:   required by substitution of ‘template<long unsigned int ..._Ind> decltype (std::__invoke(_S_declval<_Ind>()...)) std::thread::_Invoker<std::tuple<void (*)(void (&)()), void (*)()> >::_M_invoke<_Ind ...>(std::_Index_tuple<_Ind1 ...>) [with long unsigned int ..._Ind = {0, 1}]’
gcc-7.2.0/include/c++/7.2.0/thread:240:2:   required from ‘struct std::thread::_Invoker<std::tuple<void (*)(void (&)()), void (*)()> >’
gcc-7.2.0/include/c++/7.2.0/thread:127:22:   required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(void (&)()); _Args = {void (&)()}]’
thread_wrap.cpp:15:130:   required from ‘AppThread::AppThread(Function&&, Args&& ...) [with Function = void (&)(); Args = {}]’
thread_wrap.cpp:24:41:   required from here
gcc-7.2.0/include/c++/7.2.0/bits/invoke.h:89:5: error: no type named ‘type’ in ‘struct std::__invoke_result<void (*)(void (&)()), void (*)()>’

绝对可以在这里使用一些洞察力! 谢谢

最佳答案

以下是我认为 Sam 的目标示例。如您所见,它只有几行代码,所以我就此打住。

#include <iostream>
#include <thread>
#include <functional>

void preamble (void) { std::cout << "preamble\n"; }

template <class F, class ... Args> std::thread ThreadWrapper (F f, Args&& ... args)
{
    return std::thread ([f, args...] () { preamble (); f (std::forward <Args...> (args...)); });
};

int main()
{
    std::thread t = ThreadWrapper ([] (std::string s) { std::cout << s << "\n"; }, "42");
    t.join ();
}

输出:

preamble
42

Live demo

关于c++ - 如何正确扩展 std::thread 以包装生成的线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57485890/

相关文章:

c++ - 当以太网线为插件时执行 C++ 程序

c++ - 使用函数构造 std::thread

c++ - 为可变参数模板编写基本案例

c++ - 值得产生一个新线程的最小工作量

c++ - Clang:x86 FPU 调用约定

c++ - 如何访问线程外的线程数据

c++ - 如何以独立于平台的方式设置对齐方式?

具有多个包的c++模板,通过参数和返回值进行推导

c++ - unique_lock 与使用互斥锁相比有什么特殊用途?

C++ lambda函数调用纯虚函数