C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 01-07-2007, 11:46 AM   #1
Registered User
 
Join Date: Jan 2007
Posts: 6
How to stop an infinite cyle?

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!
opsis is offline   Reply With Quote
Old 01-07-2007, 11:58 AM   #2
and the hat of Jobseeking
 
Salem's Avatar
 
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.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Old 01-07-2007, 12:07 PM   #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   Reply With Quote
Old 01-07-2007, 12:57 PM   #4
Cat without Hat
 
CornedBee's Avatar
 
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   Reply With Quote
Old 01-07-2007, 12:58 PM   #5
and the hat of Jobseeking
 
Salem's Avatar
 
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.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 04:46 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22