POSIX Message Queues and Threads

Here’s my problem in a nutshell: it looks like Message Queues created
on the fly are not visible to other existing threads.

I have a main process that creates a Msg Q, Sends a message to it,
then makes a child thread. The child can receive the message without
problem.

The child makes another Msg Q and Sends a message to it. The Msg Q
descriptor is stored in a global location. When the main process tries
to do mq_getattr() on the msg q, it gets the error “doesn’t represent
a valid message queue”.

I’ve synchronized the 2 processes with a semaphore, so the Q
definitely exists before Main tries to use it.

I’d appreciate some enlightenment.

Thanks,
Paul McGlynn

In article <3a310d3d.89533352@inn.qnx.com>,
Paul McGlynn <pmcglynn@broadcom.com> wrote:

Here’s my problem in a nutshell: it looks like Message Queues created
on the fly are not visible to other existing threads.

I have a main process that creates a Msg Q, Sends a message to it,
then makes a child thread. The child can receive the message without
problem.

The child makes another Msg Q and Sends a message to it. The Msg Q
descriptor is stored in a global location. When the main process tries
to do mq_getattr() on the msg q, it gets the error “doesn’t represent
a valid message queue”.

I’ve synchronized the 2 processes with a semaphore, so the Q
definitely exists before Main tries to use it.

I’d appreciate some enlightenment.

QNX4 threads share memory space, but not file descriptors.
This means any file descriptor-based resource - e.g. message queue –
that is opened isn’t available to threads created prior to opening
the resource.

Therefore, when the second thread tries to access the file descriptor,
it may refer to (a) nothing, since the file descriptor isn’t in
use by the second thread, or (b) a completely different resource
if the second thread opened a file or device.

Any way around this would have to involve some kind of duplication
of the file descriptor into the descriptor space of the second
thread with a qnx fd creation call followed by sending an _IO_DUP
message. This is non-trivial. Sorry, I don’t have sample code handy
to refer to.

If you are hanging different queues off global variables, wouldn’t
it be easier to create the message queues up front so they would be
visible to both threads (i.e. inherited via tfork or _beginthread)?

Thanks,
Paul McGlynn

Steve Furr email: furr@qnx.com
QNX Software Systems, Ltd.