0

There is a struct defined to be used for IPC:

struct shmbuf {
  mutable std::mutex mtx;
  std::condition_variable cv;
  bool inited;
};
struct shmbuf* mySHMBuf;

I only have a pointer to it, as you can see, and that pointer is assigned as below:

mySHMBuf = mmap(NULL, sizeof(struct shmbuf), PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);

But in this case it seems that the std::condition_variable is not working at all (it stucks in wait and never wakes up, in case someone else calls mySHMBuf->cv.notify_all()).

I guess in this case the condition variable is simply not initialized properly, but as it doesn't have copy constructor, I can't assign anything else to it.

Can I somehow solve this, or what my options are? Don't use it in these situations? Use it as a pointer (bad feeling)?

If more details needed, my initial sample was from here: https://man7.org/linux/man-pages/man3/shm_open.3.html

It is pure C, doesn't use any C++ features.

Shall I stick with old C-style semaphores, just like in the example?

Daniel
  • 1,887
  • 2
  • 18
  • 40
  • 4
    Since your `struct` uses C++ classes you need to ensure that their constructors are called. This can be done via placement `new` – UnholySheep Oct 25 '21 at 17:32

0 Answers0