Thread: How to stop an infinite cyle?

  1. #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!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #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).

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2009, 11:24 PM
  2. fscanf %s or %d integer input space char stop question...
    By transgalactic2 in forum C Programming
    Replies: 5
    Last Post: 04-14-2009, 10:44 AM
  3. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM
  4. Telling other applications to stop
    By nickname_changed in forum Windows Programming
    Replies: 11
    Last Post: 09-25-2003, 12:47 AM