Her are some lines of code as principle solution:
while( !isQueueEmpty() ) {
Msg *msg = getMessage();
if( msg->send() == OK ) {
RemoveMsgFromQueue(msg);
}
};
I hope you get it ;-)
(that the answer is at the same level as your question)
tip: But it makes no sense to write a program without the use of some libraries.