linux, pthreads and message queues, thread safe

linux, pthreads and message queues, thread safe

Post by smoog » Tue, 17 May 2005 15:27:54


Hi

Are message queues (msgrcv, msgsnd) thread safe in linux pthreads?

For example:

int k1, k2;

void *thread_main(void *k) {
for (;;) {
//each thread uses unique message type (for example pid
//of external process)

//thread receives messages from other processes
msgrcv(k1, ...);
[...]
//thread sends message to other processes
msgsnd(k2, ...);
[...]
}

int main(int argc, char **argv) {
k1 = msgget(KEY_1, IPC_CREAT);
k2 = msgget(KEY_2, IPC_CREAT);

pthread_t threads[10];
for (int i = 0; i < 10; i++)
pthread_create(&threads[i], thread_main, ...);

/* do something in infinite loop */
}

Does it works?
 
 
 

linux, pthreads and message queues, thread safe

Post by Loic Domai » Tue, 17 May 2005 17:18:17

Hi!


Yes, they are.

However, notice that you might be suject to race condition if, for
instance, two threads try to pick-up the same message type at about the
same time.

But from your description, that kind of problem doesn't seem to occur,
since each thread uses unique message type.

Regards,
Loic.