我想使用Visual Studio 2010的并发库在线程之间传递操作。
我有我的班级SimpleAction
,指向它的指针存储在Concurrency::concurrent_queue
。
使用这个定义和'消费'逻辑它起作用:
typedef Concurrency::concurrent_queue<SimpleAction *> ActionQueue;
while (true)
{
SimpleAction *action = nullptr;
while (m_queue.try_pop(action))
{
action->process();
delete action;
}
Sleep(100);
}
但是,当我将其更改为std :: unique_ptr时,如下所示:
typedef Concurrency::concurrent_queue<std::unique_ptr<SimpleAction>> ActionQueue;
while (true)
{
std::unique_ptr<SimpleAction> action;
while (m_queue.try_pop(action))
{
action->process();
}
Sleep(100);
}
编译器提供以下错误消息:
F:\DevStudio\Vs2010\VC\INCLUDE\concurrent_queue.h(366) : error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>' with [ _Ty=`anonymous-namespace'::SimpleAction ] F:\DevStudio\Vs2010\VC\INCLUDE\memory(2347) : see declaration of 'std::unique_ptr<_Ty>::unique_ptr' with [ _Ty=`anonymous-namespace'::SimpleAction ] F:\DevStudio\Vs2010\VC\INCLUDE\concurrent_queue.h(365) : while compiling class template member function 'void Concurrency::concurrent_queue<_Ty>::_Copy_item(Concurrency::details::_Concurrent_queue_base_v4::_Page &,size_t,const void *)' with [ _Ty=std::unique_ptr<`anonymous-namespace'::SimpleAction> ] test.cpp(138) : see reference to class template instantiation 'Concurrency::concurrent_queue<_Ty>' being compiled with [ _Ty=std::unique_ptr<`anonymous-namespace'::SimpleAction> ]
似乎编译器不喜欢concurrent_queue中的这种结构:
/*override*/ virtual void _Copy_item( _Page& _Dst, size_t _Index, const void* _Src )
{
new( &_Get_ref(_Dst,_Index) ) _Ty(*static_cast<const _Ty*>(_Src));
}
这似乎是合乎逻辑的(我们不希望复制std :: unique_ptr(必须移动它)。
问题:
感谢, 帕特里克