开发者

Synchronization primitive for shared usage counter?

Is there a Windows (or .NET) synchronization primitive that:

  • can be shared across multiple processes on the same PC;
  • represents a counter of how many threads are currently depending on a shared resource;
  • is automatically decremented by the OS when a process abnormally terminates; and
  • can be waited on by another process (and signaled when the count drops to zero)?

I want to share a resource across multiple processes, and then close the resource when the last process exits (whether normally or ab开发者_开发百科normally) or declares that it's done with the resource. There would have to be some handshaking on startup, but that's doable; the interesting part is waiting for the processes to exit.

As far as I can tell, a named semaphore (with a high initial count) could satisfy all but the last requirement. But semaphores count down instead of up, and I don't see a way to wait for one to reach its maximum count.

If no primitive exists for this, is there a .NET library that creates something like it, or information on how to build one?

(This is for a possible implementation for How can I wait until a Windows process, and its subprocesses, have all exited?. But whereas that question has many possible approaches, here I specifically want to explore whether synchronization objects are up to the task. They're the first approach that occurred to me, but I'm hoping to learn something here about whether they really would work for this.)


There are no primitives that satisfy all these conditions. You'd have to build your own cross-process equivalent of CountdownEvent.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜