2017-07-29 69 views
0

我想通过引用传递给线程,一个变量定义为:不能按引用传递到线程

zmq::context_t context(1); 

这样的:

t[thread_nbr] = std::thread(worker_routine, (void *)&context, trained_images); 

然而,当我做我得到的以下错误:

/usr/include/c++/5/functional:1505:61: error: no type named ‘type’ in ‘class std::result_of<void* (*(void*, std::vector<TrainedImage>))(void*, std::vector<TrainedImage>&)>’ 
    typedef typename result_of<_Callable(_Args...)>::type result_type; 
                 ^
/usr/include/c++/5/functional:1526:9: error: no type named ‘type’ in ‘class std::result_of<void* (*(void*, std::vector<TrainedImage>))(void*, std::vector<TrainedImage>&)>’ 
    _M_invoke(_Index_tuple<_Indices...>) 

如果我尝试做std::ref()有了它,我收到了删除功能的错误。

有谁知道我能做什么?

+2

是什么'worker_routine'的签名? – user463035818

+5

[Off Topic]当你使用'std :: thread'时,你真的不需要将任何东西投射到'void *'。它被设计用于类型系统。 – NathanOliver

+1

您可以尝试创建一个[最小,完整和可验证示例](http://stackoverflow.com/help/mcve)并向我们展示?包括'worker_routine'和'trained_images'的声明(至少)。 –

回答

2

这个问题似乎是你的线程函数作为参数引用的参数trained_images。与此问题是std::thread对象不能真正处理引用(它副本参数的线程函数,并且引用不能被复制)。

的解决方案是使用包装对象像std::ref为引用:

t[thread_nbr] = std::thread(worker_routine, &context, std::ref(trained_images));