title: Semaphore scope and behavior
body: <p>I learned about semaphores from an earlier question I had today, and I stll am scratching my head here.  There seems to be no discussion about scope beyond global and local, where global is defined as the entire operating system.</p>
  
  <p>If I have an application made from several assemblies, and each assembly has several classes, and each class has a private static semaphore object, with different "queue" lengths, if I start queuing different tasks up in my application thread pool in different places, how does that work?  How do the threads behave around each other?  All the examples I see include one or two classes in one assembly, and I'm not getting a clear picture on how this works.</p>
  
  <p>I use thread pooling all over my app.  It parallelizes data (sending customized emails to various people, generating customized reports en masse, collecting data from various web services, etc) while leaving my interface responsive, which is a wonderful thing.</p>
  
  <p>One of my web service sources limits me to five concurrent connections, and I could not figure out how to limit the web requests to 5 active threads while still allowing the rest of the application to utilize other threads as necessary.  So, I turned to SO, and asked how to do it.  The proposed answer was use Semaphores.</p>
  
  <p>Until that point, I did not know a thing about semaphores, so I researched it.  It does indeed seem to limit the number of threads executing a specific method, but it does not make sense how this communicates properly with the thread pool manager.  If I implemented a semaphore on my web request functionality, and I get a backlog of threads waiting to perform web service calls, how does the thread pool know (can it know?) to issue more threads for other processes?  The scope of the semaphore is private; it shouldn't see the object.</p>
  
  <p>Further, is that what the semaphore is supposed to do?  Can I likewise limit other groups of tasks, by having them share a common semaphore?  Is this a gross bastardization of the intent of a semaphore, or exactly what it's meant to do.  There's so much information out there on it, but in simplified, abstract form, and I couldn't find an article describing when and how it is appropriate to use these things.</p>
  
  <p>So how does a private static semaphore communicate with the thread pool so the thread pool knows whether or not to spawn another worker thread?  Does it?  Will I be creating more problems than solutions by doing this?  What sort of behavior can I expect my thread pool to exhibit with a backlog of web requests?  Will it spawn new threads for the web requests until it's "full", reducing the thread availability for other methods?  Can I make it not do that?</p>