Note that there are some explanatory texts on larger screens.

plurals
  1. POtiemout for a function that waits indefiinitely (like listen())
    text
    copied!<p>I'm not quite sure if it's possible to do what I'm about to ask so I thought I'd ask.</p> <p>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 <code>pthread_exit()</code>. One of the threads contains <code>listen()</code> 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 <code>listen()</code> and can't move beyond.</p> <pre><code>while(1) { listen(); ... if(value == 1) pthread_exit(NULL); } </code></pre> <p>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 <code>listen()</code> 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.</p> <p>EDIT: just to clarify a little, the reason <code>listen()</code> is in the while loop is that this is a server and will be connected to multiple clients at a time.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload