J. Doe
J. Doe

Reputation: 345

What to do when done using POSIX QUEUE

I am using mqueue.h to use POSIX message queue to communicate between threads (for school project demonstration).

When I am done with my two pthreads using the queue and want to close the message queue, what should I do?

  1. Do mq_unlink and mq_close from both threads
  2. Do mq_unlink and mq_close from one thread
  3. Only do mq_unlink from one thread
  4. Only do mq_unlink from two threads
  5. Only do mq_close from one thread
  6. Only do mq_close from two threads

Edit (because of): "Closed. This question needs details or clarity"

I am using a POSIX message queue defined in mqueue.h (C) to send messages between threads. This is similar to interprocess communication with a message queue. I could communicate using the shared memory but that is not what I want to do. I have created, opened, sent and received messages between threads successfully but need to know what to do when done. I have found mq_close and mq_unlink but have not found information about how they should be used and from where. That is what I am asking about.

Upvotes: 1

Views: 2304

Answers (1)

3CxEZiVlQ
3CxEZiVlQ

Reputation: 38341

mq_overview - overview of POSIX message queues.

Similar like dealing with files.

Call mq_close on each mq_open.

When a process has finished using the queue, it closes it using mq_close(3), and when the queue is no longer required, it can be deleted using mq_unlink(3).

Call once mq_unlink optionally.

POSIX message queues have kernel persistence: if not removed by mq_unlink(3), a message queue will exist until the system is shut down.

mq_unlink() removes the specified message queue name. The message queue name is removed immediately. The queue itself is destroyed once any other processes that have the queue open close their descriptors referring to the queue.

Upvotes: 3

Related Questions