Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a ConcurrentQueue of objects in managed c++/.net 4.0

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?

like image 830
user1850479 Avatar asked Jan 17 '26 20:01

user1850479


1 Answers

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.

like image 186
Pragmateek Avatar answered Jan 19 '26 15:01

Pragmateek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!