Re: Linus on Linux, Apache and Threads

Ian D Romanick (idr@cs.pdx.edu)
Fri, 23 Apr 1999 11:34:10 -0700 (PDT)


> On Fri, 23 Apr 1999, Ian D Romanick wrote:
> > Huh? It's not that hard of a problem. You have one (or several) thread
> > that just reads from network sockets. They package up the requests and put
> > them on the end of a queue. The other threads just pull requests off the
> > head of the queue.
>
> i wasn't trying to suggest it was hard to code or understand. my question
> is how to do this efficiently. has anyone compared the performance of
> this model with the performance of the same application implemented
> using NT's completion ports?
>
> every Unix model i've seen that uses the "single thread waits for event
> and wakes up worker threads" has suffered from a variety of problems that
> can cripple its performance, like:
>
> how does the event waiter thread determine whether there is a
> suitable/idle worker thread to awaken?
>
> > The trick is all in waking up the sleeping threads when
> > the queue becomes non-empty without having the "thundering herd" problem.
>
> agreed! any ideas?

It could be done several ways. Does Linux have Tanenbaum style up/down
semaphores? If not, it shouldn't be too hard to do using pthread_mutex.
Each time an element is put on the event queue, you up the semaphore.
Before getting the dequeue lock on the queue, the thread would down the
semaphore.

At this point, you have a pretty easy model, and all of the hard work is in
implementing the down function. I would say that the semaphore could be
implemented with a lock, a counter, and a queue of pthread_t objects. The
trick is that when you down a semaphore that is already zero, you put the
thread on the queue and have it go to sleep. Then when you up a semaphore
that has sleeping threads, you wake up the first thread.

The problem is that I don't know how to do the whole sleep/wake up thing
with pthreads. It seems as though it could be done condition variables, but
then you still have the thundering herd problem. I suppose that you could
allocate one condition variable per thread, but that doesn't seem very
elegant either.

-- 
"With a touch more confidence and a liberal helping of ignorance I would have 
been a famous evangelist."
                        -- Stranger In A Strange Land
PLENTY of ignorance at http://www.cs.pdx.edu/~idr

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu Please read the FAQ at http://www.tux.org/lkml/