0

There's a well-known algorithm that employs readers/writer lock synchronization between threads of a single process on a Windows platform using pure WinAPIs/C++:

Here's an example

In my case I need to do this between several processes, i.e. the writer is in one process, and readers are in other processes. Any idea how to do that?

PS. I need this for an already developed project, so I can't use anything other than C++/MFC or pure WinAPIs. In other words I can't use Boost or the like extensions.

ahmd0
  • 16,633
  • 33
  • 137
  • 233

1 Answers1

3

You can use the same algorithm but instead of CriticalSection you can use Mutexes from the WinAPI.
If you use the same name for your Mutex objects you can use them in several processes.

mkaes
  • 13,781
  • 10
  • 52
  • 72
  • Thank you. Global named mutexes will indeed solve the critical section problem. But my main concern is the 'm_cReaders' counter, since EnterReader()/LeaveReader() will be called from different reader processes. So how do you implement that? – ahmd0 Jun 16 '12 at 20:07
  • @ahmd0: Just put your counter in a [shared memory area](http://msdn.microsoft.com/en-us/library/windows/desktop/aa366551%28v=vs.85%29.aspx). Give your event a name and then it should work. – mkaes Jun 17 '12 at 09:32
  • Yes, I'm doing it. But I've encountered one issue with such approach. If I simply increment or decrement the reader counter like they do in that sample, I can run into a "situation" if one of the reader processes crashes and doesn't decrement its counter. This situation will deadlock my mechanism. – ahmd0 Jun 17 '12 at 19:44