View Full Version : How to stop an infinite cyle?

01-07-2007, 11:46 AM
Hi there!

I have a program waiting for messages from another process and it must have an infinite loop like for(;;) but I wouldn't like to have it using 100% CPU while it doesn't have messages to read.

How can I solve this problem? The messages are sent via shared memory.

I've been trying to use pthread_cond (since the reading part is in another thread). But the thread with an infinite loop is the one that can see when there is or not a new message.

Thanks in advance!

01-07-2007, 11:58 AM
Some reading - http://www.llnl.gov/computing/tutorials/pthreads/
The sending thread writes to shared memory (why not use thread messages?), and sets the condition variable.
The receiving thread waits for the condition variable, then reads shared memory.

01-07-2007, 12:07 PM
No, you got me wrong...

I have a sending *process* and a receiver process with a receiving thread. That's why I can't use it (I guess).

01-07-2007, 12:57 PM
You could use a SysV message queue simply for the purpose of making one process wait for another.

01-07-2007, 12:58 PM
Are you doing anything else if a message cannot be read?

Consider using say usleep() to allow you to poll for a message, then wait for a short while.

Or maybe use a pipe between the two processes which can be used to signal that a message is available.