Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use C++20 `std::atomic<T>::wait()` or `std::atomic_flag::wait()` in shared memory?

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
like image 894
itzjackyscode Avatar asked Sep 16 '25 11:09

itzjackyscode


1 Answers

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.

like image 199
itzjackyscode Avatar answered Sep 19 '25 00:09

itzjackyscode