Everyone
Everyone

Reputation: 1825

Windows Handle wait until all references are released

I have multiple processes in an IPC system that I am designing. Each process creates a FileMapping and MapViewOfFile as its own memory area. Also, each process creates two semaphores that manage the FileMapping and MapViewOfFile that it already created.

To describe the problem I have, assume 3 processes:

  1. ProcessA creates FileMapping, MapViewOfFile and the two semaphores.

  2. ProcessB and ProcessC do similar to ProcessA.

  3. ProcessA wants to send something to ProcessB. It connects to the FileMapping and MapViewOfFile and the two semaphores of ProcessB and sends whatever it wanted to send.

  4. ProcessB wants to reopen its communication. It closes its FileMapping and MapViewOfFile and its two semaphores. It sends closing message to Processes A and C. When Processes A and C receive the closing message, they will close handles to FileMapping, MapViewOfFile and the two semaphores associated with ProcessB.

  5. ProcessB closed all its handles and wants to reopen. When it tries to create MapViewOfFile it fails since Processes A and C have not closed the handles to ProcessB yet.

Right now (as a temporary fix), I am letting ProcessB sleep for 100ms when it is closing in order to give time for Processes A and C to close their handles to ProcessB. However, I want a solution that does not involve sleeping.

Is there a way for ProcessB to know when all references to its handles (FileMapping, MapViewOfFile and the two semaphores) are released by all other processes? If there is, how can I wait on it?

Upvotes: 1

Views: 339

Answers (1)

valdo
valdo

Reputation: 12943

There's definitely no winapi function to do this. You can only try to open a handle to a (named) object and see if it fails with the appropriate reason.

So the simplest workaround is to do what are you trying in a loop with Sleep(0) or something similar. You may also add an additional synchronization object for this specific purpose, an auto-reset named event which your ProcessB may open and WaitFor.....

Upvotes: 1

Related Questions