I've been trying to update some managed c++ code I inherited. I don't know any c++, but I took a c# class in back in the 1.1 days, so I can kind of find my way around .Net. So far, I've had good results using ConcurrentQueue's to send work to worker threads from my main thread:
fullQueue = gcnew ConcurrentQueue<int>();
..
fullQueue->Enqueue(someNumber);
Now I'd like to try inserting actual objects so that I can send workers more complicated instructions. However, this does not work:
public ref class workUnit
{
int ptrOffset;
System::String^ outputPath;
public:
workUnit(int offset, System::String^ path)
{
ptrOffset=offset;
outputPath=path;
}
};
..
ConcurrentQueue test = gcnew ConcurrentQueue<workUnit ^>();
I get:
'System::Collections::Concurrent::ConcurrentQueue' : use of class generic requires generic argument list
'System::Collections::Concurrent::ConcurrentQueue::ConcurrentQueue' : the function template cannot convert parameter 1 from type 'System::Collections::Concurrent::ConcurrentQueue<T> ^'
Clearly I'm missing something fundamental about how objects are inserted into the queue. In my head, I think I'm making a queue that will hold references to class objects I can instantiate later, so the CLR should just need to know what reference type will go in, but apparently this is not correct. What am I missing?
Seems like you've missed something :)
using namespace System::Collections::Concurrent;
ref class WorkUnit
{
};
int main()
{
ConcurrentQueue<WorkUnit^>^ test = gcnew ConcurrentQueue<WorkUnit^>();
}
You must reference the generics type <WorkUnit^> in both sides of the initialisation expression.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With