views:

23

answers:

1

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 abnormally) 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 http://stackoverflow.com/questions/3233434/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.)

+1  A: 

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

Stephen Cleary