4

My project involves a plugin, and a GUI for said plugin which is isolated into a separate process. The data I'm sharing may be updated by the GUI, and when it is, it should be processed by the plugin.

To do this, I'm considering putting this in my shared-memory block:

std::atomic_bool event_flag; // insert mutex... some_data_struct data; 

In essence, the GUI does the following when it wants to change the data:

// acquire mutex // write data... // release mutex event_flag = true; event_flag.notify_one(); 

Then the plugin does the following:

event_flag.wait(true); event_flag = false; // acquire mutex // read data... // release mutex 
3
  • How are you going to create the objects in the shared memory such that they are valid in both processes? Placement new comes to mind but I don't know what will happens if two processes try this on the same block of memory. This could work if the atomics are "ImplicitLifetimeType" en.cppreference.com/w/cpp/named_req/ImplicitLifetimeType Commented Sep 22, 2022 at 2:22
  • @RichardCritten Shared memory, created in Linux with shm_open() and resized with ftruncate(), is automatically initialized to zero. I have defined a struct with a lot of std::atomic_flag and load a pointer with the address of the shared memory. This is sufficient, as long as zero is valid for all types of objects in the shared memory struct. Commented Apr 25, 2024 at 14:07
  • @S.Gleissner This will only not be UB for C++ types the can be created with implicit lifetimes. Commented Apr 25, 2024 at 14:25

1 Answer 1

3

The C++ standard never specified how C++ code interacts with shared memory and processes, so much of this is implementation-defined.

However, it seems that implementations are not cross-process:

  • libstdc++ seems to use futexes and/or condition variables with tables. Condition variables are likely not shared across processes, but I haven't bothered to check.
  • Microsoft's STL uses futexes, which cannot work across processes.
  • libc++ is likely similar, and I have not bothered checking.

There is a proposal for process management, but that hasn't gone too far, given that the C++ standard doesn't have a concept of "processes".

I will edit this answer if such behaviour is eventually specified.

Sign up to request clarification or add additional context in comments.

1 Comment

Linux futexes (as used by libstdc++) are cross process.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.