简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM