Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ensure only one process accesses shared memory at a time

I am fairly new to Win32 programming. I want to create a shared memory between two processes. I already have created shared memory using the Memory Mapping function.

My struct would look this this:

struct sharedMemory
{
  int ans1;
  int ans2;
  BOOLEAN flag1;
};

Now I am able to access this shared memory from different processes, but I am confused on how to apply lock to this shared memory so that only 1 process is able to access the struct member variable.

like image 636
rohit sharma Avatar asked Oct 14 '25 08:10

rohit sharma


1 Answers

By providing a mutex name when calling CreateMutex, you'll make the mutex visible from other processes.

If another process passes the same name to the CreateMutex, it will get the existing mutex (instead of creating a new one), which can then be used for inter-process synchronization.

like image 181
Branko Dimitrijevic Avatar answered Oct 17 '25 03:10

Branko Dimitrijevic