I need your help regarding the message acknowledgement in the queue when the message reaches at the queue. Any help would be highly appreciated.
the code for my queue class is;

struct ListNode
{
    ListNode(gcroot<message^> msg);
    gcroot<message^> msg;
    ListNode* next;
}; 
ListNode::ListNode(gcroot<message^> msg): msg(msg), next(nullptr) {}

public class queue
{
public:
    queue();
    ~queue();
    bool Empty() const;
    void Enqueue(message^);
    void Dequeue();
        void DisplayAll() const;

private:
    // Disable copying to keep the example simple
        gcroot<String^> bindingkey;
        queue(queue const& obj);

    ListNode* head;
    ListNode* tail;
    gcroot<String^> name; // Added this
};

queue::queue():head(nullptr), tail(nullptr) {}

queue::~queue()
{
    while (head != nullptr)
    {
        Dequeue();
    }
}

void queue::Enqueue(message^ key)
{
    if (head == nullptr)
    {
        head = tail = new ListNode(key);
    }
    else
    {
        tail->next = new ListNode(key);
        tail = tail->next;
    }
}

void queue::Dequeue()
{
    ListNode* temp = head->next;
    delete head;
    head = temp;
}

void queue::DisplayAll() const
{
    for (ListNode* p = head; p; p = p->next)
    {
                Console::WriteLine("the element is {0}", p->msg->routingk);
    }

}

Message.h;

public ref class message
{
 public:

  property System::String^ routingk;

  message(String^ Id)
  { 
      routingk = Id;
  }

  System::String^ message::getType()
  {
   return routingk;
  }


};

I need your help regarding the message acknowledgement in the queue when the message reaches at the queue.

Sorry, no idea what this means...

Well, there are a couple of things you can do. You can poll the queue repeatedly, checking to see if it's not empty.

void someFunction(){
    while(1){//Loop forever.
        if(myQueue.Empty()==false){
            doSomethingAmazing(myQueue.popFront());
        }
    }
}

This is kind of wasteful.
Or you can put in an observer-relationship. You create an observer object list in your queue and make assignments of who's watching it. Once a message is received, you update all the observers with the message.

void queue::Enqueue(message^ key)
{
    if (head == nullptr)
    {
        head = tail = new ListNode(key);
    }
    else
    {
        tail->next = new ListNode(key);
        tail = tail->next;
    }
    //Loop through and update all observer objects
    for(int i=0; i<observers->Length; i++)   
        observers[i]->update(key);
}

However, this is also not super useful as you defeat the purpose of having a queue by updating everything at once AND you risk missing messages while updating.

So essentially, I've given you two bad ideas, but I'm leading up to the good idea.
Which I'm going to tell you about after lunch. (Hint: It involves threading.)

The way to do this is to run a thread that handles updating the observers. The thread itself waits for a semaphore linked to the queue. Every time, you enqueue a new message, you increment (release) the semaphore.

This link may be helpful:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms686946

As a bonus, if you're going to run a thread specific to the queue, create a static "runThread" method in the class that takes the object pointer (this) as it's parameter. This will save you a few hours of headaches.

Edited 4 Years Ago by DeanMSands3

thanks for the reply but how can I use that. can you help me using this. I dont know semaphores and threads :(

Yeah, I was worried that answer would be completely useless. Let me ask this: How are you getting your messages and what are you using them for?

I dont know semaphores and threads

Then start learning. Read up on them then write a small program that illustrates how they work.

This article has been dead for over six months. Start a new discussion instead.