![]() |
| | #1 |
| Registered User Join Date: Jan 2007
Posts: 6
| How to stop an infinite cyle? 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! |
| opsis is offline | |
| | #2 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,699
| 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. |
| Salem is offline | |
| | #3 |
| Registered User Join Date: Jan 2007
Posts: 6
| 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). |
| opsis is offline | |
| | #4 |
| Cat without Hat Join Date: Apr 2003
Posts: 8,492
| You could use a SysV message queue simply for the purpose of making one process wait for another.
__________________ All the buzzt! CornedBee"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code." - Flon's Law |
| CornedBee is offline | |
| | #5 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,699
| 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. |
| Salem is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to stop an infinite loop by means of any key? Please help. | MarkSquall | C Programming | 2 | 06-14-2009 11:24 PM |
| fscanf %s or %d integer input space char stop question... | transgalactic2 | C Programming | 5 | 04-14-2009 10:44 AM |
| when a while loop will stop ? | blue_gene | C Programming | 13 | 04-20-2004 03:45 PM |
| Telling other applications to stop | nickname_changed | Windows Programming | 11 | 09-25-2003 12:47 AM |