I am trying to use the mq_notify call to initiate a thread with POSIX message queues. Everything seems right but I only get one thre开发者_运维问答ad and it process all messages but it never terminates. I was under the impression I would get new threads to process with and this doesn't seem right.
First, mq_notify() will only start one thread at a time. Second, threads are not started each time a message is put in the queue; they are only started when a queue that has been empty gets a new message. Third, mq_notify() is a one-shot deal. Once a thread is fired off, if you want to fire a new one the next time an empty queue receives a message you need to re-register with mq_notify(). Commonly one of the first things your threadfunc should do is re-register.
Since you are starting a thread and processing messages my guess is that you did not open the queue as O_NONBLOCK or reset it to such with mq_setattr(). When a thread fires you want to drain the queue of all messages so another thread can start up next time around. If the queue is in blocking mode you are going to block after the last message has been received and the thread will never end. In non-blocking mode you want to keep reading until you get errno == EAGAIN and then end the thread. You should be good to go for the next batch of messages with a new thread.
精彩评论