Find the answer to your Linux question:
Results 1 to 3 of 3
Hello all, I have a pthread waiting forever on a POSIX message queue and then call mq_close and mq_unlink on the POSIX message queue. I've found that the pthread never ...
  1. #1
    Just Joined!
    Join Date
    Apr 2011
    Posts
    4

    Problem closing/unlinking POSIX message queues

    Hello all,

    I have a pthread waiting forever on a POSIX message queue and then call mq_close and mq_unlink on the POSIX message queue. I've found that the pthread never wakes up from it's call to mq_receive and remains blocked indefinitely. Is there a way to wakeup all pthreads blocked on a POSIX message queue after calling mq_close/mq_unlink? The goal is to include error handling during message queue deletion to avoid leaving any pthreads blocked forever.

    Thanks,
    Shaun

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    The quick answer is "it depends". Did each thread open the queue? Or only one? What about the other end of the queue? How are you using the queue?

    In any case, one solution is to use a timer interrupt so that each thread gets bounced out of the call after some period of time.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Apr 2011
    Posts
    4
    Hi Rubberman, thanks again for taking the time to respond! I'll try to elaborate below.

    In this code, one thread can open a message queue but any thread may close and unlink it. In between, multiple threads may send/receive to the message queue. This is possible with a target API I'm emulating on Linux. A side goal of the emulation is to avoid modifying application code which calls the API and directly handle these details inside the emulation layer. The part of this API I'm currently focused on is how to unblock waiting threads when the message queue is 'deleted', for which I'm using mq_close/mq_unlink.

    Back to the timer idea, I may not understand fully - can you explain this a bit? My sw executes in user-space so do you mean POSIX timers? The background is that in the target API, I need to support a wait forever option so I cannot set an upper bound on the timeout value. Also, in the mq_receive case, I'm not quite sure how to bounce the threads. The approach which comes to mind is:

    lock message queue to prevent other threads from receiving on it
    get count of waiting threads - (not sure how...)
    while (count--)
    send 'mq destroyed message'
    unlock message queue

    There's also the question of how to unblock threads blocked on mq_send, but I'm not quite at that point in my testing.

    The idea I have at the moment is to implement my own message queues using POSIX condition variables for waiting/unblocking. Then I can pthread_cond_broadcast to all waiters on message queue deleting. I'd love if there was a way to do this with the POSIX API. But, POSIX does not appear to define the behavior of message queue deletion on waiting threads, and it seems hanging threads are a possibility.

    Thanks again for your advice.

    Best,
    Shaun

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...