Message queue problem

Hi all,

I have a message queue that I use for placing messages that are destined for processes that have not yet started up. I call this my NULL message queue.
When a process later starts up a message queue for that process is created. Once created I want to search thought the NULL message queue for messages that are meant for it. But how can I search thought this NULL queue without losing messages?

I was thinking something like (this is peudo code):

Get number of messages in NULL queue (using mq_getattr()); count = 0; while (count < Number of messages); Get message from NULL queue (using mq_receive); see if it for the new process (the message will have destination process ID): if it is for the new queue add it to new queue (using mq_send()); else put it back in NULL queue(using mq_send()); endif count++; end while;

Would this work? Or will I end up looking at the same message over and over again in the NULL queue?

Cheers