6

I want to use the Concurrency library of Visual Studio 2010 to pass actions between threads. I have my class SimpleAction and pointers to it are stored in the Concurrency::concurrent_queue.

Using this definition, and 'consumption' logic it works:

typedef Concurrency::concurrent_queue<SimpleAction *> ActionQueue;

while (true)
   {
   SimpleAction *action = nullptr;
   while (m_queue.try_pop(action))
      {
      action->process();
      delete action;
      }
   Sleep(100);
   }

However, when I change this to an std::unique_ptr, like this:

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);
   }

The compiler gives the following error message:

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>
    ]

It seems the compiler does not like this construction in 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)); 
}

Which seems logical (we don't want an std::unique_ptr to be copied (it must be moved instead).

Questions:

  • Is this a known problem/limitation/feature of the Concurrency/PPL library of Visual Studio 2010?
  • Is this problem solved in Visual Studio 2012?
  • Or am I doing something wrong?

thanks, Patrick

Patrick
  • 23,217
  • 12
  • 67
  • 130

0 Answers0