Pat*_*ick 6 visual-studio-2010 concurrency-runtime concurrent-queue
我想使用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);
}
Run Code Online (Sandbox Code Playgroud)
但是,当我将其更改为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);
}
Run Code Online (Sandbox Code Playgroud)
编译器给出以下错误消息:
Run Code Online (Sandbox Code Playgroud)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));
}
Run Code Online (Sandbox Code Playgroud)
这似乎是合乎逻辑的(我们不希望复制std :: unique_ptr(必须移动它).
问题:
谢谢,帕特里克
| 归档时间: |
|
| 查看次数: |
761 次 |
| 最近记录: |