On Wed, 8 Jun 2005, Arnaud Bergeron wrote:

> For a threaded network application I'm developping, I have to watch
> the number of concurrent connections.  I currently use this code:
> 
> if (curconns < maxconns) {
>       curconns++;
> }
> else {
>       pthread_cond_wait(&conns_is_at_max, &useless_mutex);
>       continue;
> }
> 
> curconns is decremented by other threads each time a connection is
> finished and is only incremented in the main thread.
> 
> I use a condition variable to know when it is decremented so that I
> can restart the loop and accept the connection.
> 
> I don't need a mutex but it seem the pthread code requires one.  The
> manpages on pthreads are very scarce.  Is there a better/simpler way
> to do this?

i don't know how you came to that conclusion, but hello race condition.  
yes, you need a mutex.  use one.

-- 
And that's why Yahoo is Morbo's Vermin of the Week.

Reply via email to