![semaphor technique semaphor technique](https://media.3rails.fr/original/3X/c/6/c69c0c39db0b1b56e497caea43d2ebe17ea0eadd.jpeg)
You can only yield, period - it's up to the Windows scheduler to decide what thread gets to run next. I'm fairly certain that there's no way to yield control to a specific thread, which seems to be what you're trying to do. I was mostly hoping there was somewhat more elegant solution was available, but I suspect that like everything else about this project, it's all brute force and corner cases :-(. I suspect that a variant of Aaron's solution will be what I use. Kill the worker thread and continue only after it is dead. There's only ever one thread executing Code B.Ĭode A: private static Thread workerThread = null There can be multiple threads executing Code A. Someone asked for more details, so here it is.
![semaphor technique semaphor technique](https://medias.audiofanzine.com/images/normal/catalinbread-stereo-semaphore-tremolo-266068.jpg)
The commenter is correct - I am using a critical section, but given that everything is running in a single process, in this example critical sections are functionally equivalent to the more general term 'semaphore'.
SEMAPHOR TECHNIQUE HOW TO
Is there a technique in C# that I can use to shut down thread B? I can set a flag in A and have thread B check that flag and exit as soon as it gets control of the semaphore, but I don't know of any technique to allow thread A to yield the semaphore to thread B (and get it back when thread B exits) without the risk of thread C seizing control.Īnyone have any suggestions how to address this design problem? I can rewrite the program as necessary if I am approaching this incorrectly.Ī commenter has pointed out that I am using the wrong terminology. Thread A holds the semaphore (using lock) and threads B and C are waiting on that same semaphore (also using lock). I have multiple threads that share use of a semaphore.