2016-02-01 52 views
1
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; 
} 

我试图编译上面的简单代码,但我不断收到错误,因为行thread thread(func_wrapper, tq);如何使用带引用的函数指针创建std :: thread?

这段代码有什么问题?

我该如何解决?

该错误消息如下:

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 
+0

请注意,此代码在行'线程线程(func_wrapper,tq)上具有未定义行为的巨大潜力;返回0; }在构造线程被加入或分离之前被销毁的地方。 – Patryk

+0

感谢您指出这一点。我简化了这个问题的代码。在我真正的应用程序代码中,线程是taskq类的成员,taskq对象将成为全局变量。 – xiaohl

回答

2
thread thread(func_wrapper, std::ref(tq)); 

请注意,您需要确保当您执行func_wrapper(因为我猜这种功能使用tq)基准仍然有效。

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