Possible Duplicate:
Limiting the number of threads executing a method at a single time
In a single app we need to limit executing a section of code to 4 threads. This section is not the start/end of the thread, it is a section of code inside the thread.
How to do this with one is easy - create an EventWaitHandle and wait/signal on it. But every way I've thought of to do 4 leaves the possibility of determining I need to wait, another thread exists the section and signals, and I then wait - forever.
So what is the most light-weight way to do this when all threads are in a single app?
Background: We are changing our licensing model to allow a maximum of N (4 in this example) threads calling our library consecutively. Because we're a library, not a server, we don't control the thread usage (like databases can). In the case of a web app, the calling app also doesn't control the number of threads and so we need to pause thread 5 calling us until one the first 4 exit that code.