简体   繁体   中英

How to create std::thread with function pointer with a reference?

class taskq {
public:
  int trigger(taskq &tq);      
  mutex mtx;
};

void func_wrapper(taskq &tq) {
  cout<<endl;      
}

int taskq::trigger(taskq &tq) {
  thread thread(func_wrapper, tq);    
  return 0;
}

I tried to compile the above simple code but I am keep getting errors because of line thread thread(func_wrapper, tq); .

What's wrong with this code?

How could I fix it?

The error message is as below:

In file included from /usr/include/c++/4.9/thread:39:0,
             from taskq.C:2:
/usr/include/c++/4.9/functional: In instantiation of ‘struct std::_Bind_simple<void (*(taskq))(taskq&)>’:
/usr/include/c++/4.9/thread:140:47:   required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(taskq&); _Args = {taskq&}]’
taskq.C:20:33:   required from here
/usr/include/c++/4.9/functional:1665:61: error: no type named ‘type’ in ‘class std::result_of<void (*(taskq))(taskq&)>’
   typedef typename result_of<_Callable(_Args...)>::type result_type;
                                                         ^
/usr/include/c++/4.9/functional:1695:9: error: no type named ‘type’ in ‘class std::result_of<void (*(taskq))(taskq&)>’
     _M_invoke(_Index_tuple<_Indices...>)
     ^
In file included from /usr/include/c++/4.9/functional:55:0,
             from /usr/include/c++/4.9/thread:39,
             from taskq.C:2:
/usr/include/c++/4.9/tuple: In instantiation of ‘constexpr std::_Head_base<_Idx, _Head, false>::_Head_base(_UHead&&) [with _UHead = taskq; <template-parameter-2-2> = void; long unsigned int _Idx = 1ul; _Head = taskq]’:
/usr/include/c++/4.9/tuple:271:42:   required from ‘constexpr std::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(std::_Tuple_impl<_Idx, _Head, _Tail ...>&&) [with long unsigned int _Idx = 1ul; _Head = taskq; _Tail = {}]’
/usr/include/c++/4.9/type_traits:900:43:   required by substitution of ‘template<class _Tp, class _Arg, class> static std::true_type std::__do_is_direct_constructible_impl::__test(int) [with _Tp = std::_Tuple_impl<1ul, taskq>; _Arg = std::_Tuple_impl<1ul, taskq>&&; <template-parameter-1-3> = <missing>]’
/usr/include/c++/4.9/type_traits:912:43:   required from ‘struct std::__is_direct_constructible_impl<std::_Tuple_impl<1ul, taskq>, std::_Tuple_impl<1ul, taskq>&&>’
/usr/include/c++/4.9/type_traits:134:12:   required from ‘struct std::__and_<std::is_destructible<std::_Tuple_impl<1ul, taskq> >, std::__is_direct_constructible_impl<std::_Tuple_impl<1ul, taskq>, std::_Tuple_impl<1ul, taskq>&&> >’
/usr/include/c++/4.9/type_traits:916:12:   required from ‘struct std::__is_direct_constructible_new_safe<std::_Tuple_impl<1ul, taskq>, std::_Tuple_impl<1ul, taskq>&&>’
/usr/include/c++/4.9/type_traits:994:12:   [ skipping 7 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]
/usr/include/c++/4.9/type_traits:1175:12:   required from ‘struct std::is_nothrow_move_constructible<std::_Tuple_impl<1ul, taskq> >’
/usr/include/c++/4.9/type_traits:134:12:   required from ‘struct std::__and_<std::is_nothrow_move_constructible<void (*)(taskq&)>, std::is_nothrow_move_constructible<std::_Tuple_impl<1ul, taskq> > >’
/usr/include/c++/4.9/tuple:267:7:   required from ‘constexpr std::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(std::_Tuple_impl<_Idx, _Head, _Tail ...>&&) [with long unsigned int _Idx = 0ul; _Head = void (*)(taskq&); _Tail = {taskq}]’
/usr/include/c++/4.9/functional:1727:41:   required from ‘typename std::_Bind_simple_helper<_Func, _BoundArgs>::__type std::__bind_simple(_Callable&&, _Args&& ...) [with _Callable = void (&)(taskq&); _Args = {taskq&}; typename std::_Bind_simple_helper<_Func, _BoundArgs>::__type = std::_Bind_simple<void (*(taskq))(taskq&)>]’
/usr/include/c++/4.9/thread:140:47:   required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(taskq&); _Args = {taskq&}]’
taskq.C:20:33:   required from here
/usr/include/c++/4.9/tuple:140:42: error: use of deleted function ‘taskq::taskq(taskq&&)’
: _M_head_impl(std::forward<_UHead>(__h)) { }
                                      ^
taskq.C:9:7: note: ‘taskq::taskq(taskq&&)’ is implicitly deleted because the default definition would be ill-formed:
class taskq {
   ^
taskq.C:9:7: error: use of deleted function ‘std::mutex::mutex(const std::mutex&)’
In file included from taskq.C:3:0:
/usr/include/c++/4.9/mutex:129:5: note: declared here
 mutex(const mutex&) = delete;
 ^
In file included from /usr/include/c++/4.9/functional:55:0,
             from /usr/include/c++/4.9/thread:39,
             from taskq.C:2:
/usr/include/c++/4.9/tuple: In instantiation of ‘constexpr std::_Head_base<_Idx, _Head, false>::_Head_base(const _Head&) [with long unsigned int _Idx = 1ul; _Head = taskq]’:
/usr/include/c++/4.9/tuple:255:44:   recursively required from ‘constexpr std::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(const _Head&, const _Tail& ...) [with long unsigned int _Idx = 1ul; _Head = taskq; _Tail = {}]’
/usr/include/c++/4.9/tuple:255:44:   required from ‘constexpr std::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(const _Head&, const _Tail& ...) [with long unsigned int _Idx = 0ul; _Head = void (*)(taskq&); _Tail = {taskq}]’
/usr/include/c++/4.9/tuple:531:30:   required from ‘constexpr std::tuple<_T1, _T2>::tuple(const _T1&, const _T2&) [with _T1 = void (*)(taskq&); _T2 = taskq]’
/usr/include/c++/4.9/functional:1678:74:   required from ‘std::_Bind_simple<_Callable(_Args ...)>::_Bind_simple(_Callable&&, _Args2&& ...) [with _Args2 = {taskq&}; <template-parameter-2-2> = void; _Callable = void (*)(taskq&); _Args = {taskq}]’
/usr/include/c++/4.9/functional:1727:41:   required from ‘typename std::_Bind_simple_helper<_Func, _BoundArgs>::__type std::__bind_simple(_Callable&&, _Args&& ...) [with _Callable = void (&)(taskq&); _Args = {taskq&}; typename std::_Bind_simple_helper<_Func, _BoundArgs>::__type = std::_Bind_simple<void (*(taskq))(taskq&)>]’
/usr/include/c++/4.9/thread:140:47:   required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(taskq&); _Args = {taskq&}]’
taskq.C:20:33:   required from here
/usr/include/c++/4.9/tuple:134:25: error: use of deleted function ‘taskq::taskq(const taskq&)’
   : _M_head_impl(__h) { }
                     ^
 taskq.C:9:7: note: ‘taskq::taskq(const taskq&)’ is implicitly deleted because the default definition would be ill-formed:
 class taskq {
   ^
 taskq.C:9:7: error: use of deleted function ‘std::mutex::mutex(const std::mutex&)’
 In file included from taskq.C:3:0:
 /usr/include/c++/4.9/mutex:129:5: note: declared here
 mutex(const mutex&) = delete;
 ^
 make: *** [taskq.o] Error 1
thread thread(func_wrapper, std::ref(tq));

请注意,在执行func_wrapper时需要确保引用仍然有效(因为我猜这样的函数使用tq )。

std::thread thread([&tq]() {
  func_wrapper(tq);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM