tiemout for a function that waits indefiinitely (like listen())

Posted by Fantastic Fourier on Stack Overflow See other posts from Stack Overflow or by Fantastic Fourier
Published on 2010-04-08T11:37:23Z Indexed on 2010/04/08 11:43 UTC
Read the original article Hit count: 234

Filed under:
|
|
|

Hello, I'm not quite sure if it's possible to do what I'm about to ask so I thought I'd ask.

I have a multi-threaded program where threads share a memory block to communicate necessary information. One of the information is termination of threads where threads constantly check for this value and when the value is changed, they know it's time for pthread_exit(). One of the threads contains listen() function and it seems to wait indefinitely. This can be problematic if there are nobody who wants to make connection and the thread needs to exit but it can't check the value whether thread needs to terminate or not since it's stuck on listen() and can't move beyond.

while(1)
{
  listen();
  ...
  if(value == 1)
    pthread_exit(NULL);
}

My logic is something like that if it helps illustrate my point better. What I thought would solve the problem is to allow listen() to wait for a duration of time and if nothing happens, it moves on to next statement. Unfortunately, none of two args of listen() involves time limit. I'm not even sure if I'm going about the right way with multi-threaded programming, I'm not much experienced at all. So is this a good approach? Perhaps there is a better way to go about it? Thanks for any insightful comments.

© Stack Overflow or respective owner

Related posts about c

    Related posts about listen