I have a case where I want to acquire (lock) a resource in a function call, but am signaled of the end of the process in a callback (different thread). (The resource is external: basically, a certain bus gets busy when I start and is free again with the callback.)
With lock/critical section this is not possible at all. I also tried Mutex, but only get exceptions, probably because I release in another thread.
What are the options here?
It seems that I can
- create bool to "manually" sync them (lock access to volatile bool, and then do a while() sleep instead of the WaitOne)
- use events to single-thread the whole thing in a 3rd wrapper thread that then also manages the synchronization object
I would probably go for the bool though for simplicity. Or preferably any mechanism provided by the runtime. The callback comes from an external library.
Update: I have also just now discovered the semaphore, which seems to fit my needs. I will ask anyone in case someone has a better idea/someone else finds this useful, too.
Monitor.Wait
andMonitor.Pulse
/Monitor.PulseAll
– Marc Gravell