1

I'm trying to solve a publisher suscriber problem using a buffer in shared memory. My Publisher writes a message in buffer from an offset (from the starting of the buffer) using memcpy() in C++ and after it has written the message it stores this offset, where it wrote the last message, in another segment in shared memory. The suscribers use this offset to determine where the last message was successfully written by publisher. The problem is that I need to control the order of stores i.e. the publisher will first successfully store the message in buffer and then update its offset. Similarly on the suscriber side I need the consumers to first successfully read a message and then update information regarding where they just read the last message. I can think of using memory barriers here but cannot implement it.

  • You could use a `std::deque` for the buffer. – Eljay Jul 02 '21 at 13:58
  • A long time ago, when dinosaurs where still roaming the Earth, we used to use semaphores for that purpose. See [man sem_overview](https://man7.org/linux/man-pages/man7/sem_overview.7.html) – YSC Jul 02 '21 at 14:02

1 Answers1

0

I think you are confusing memory barriers with locks. Usually producer/consumer pattern is implemented using some form of locking.

Also the term "shared memory" is usually used when you have multiple processes because all memory is "shared" in the same process.

You can find more info about the pattern here: https://en.wikipedia.org/wiki/Producer%E2%80%93consumer_problem and more info about the difference between the different types of locking is here: difference between lock, memory barrier, semaphore

This is a complicated problem and it requires experience to do it right. If you want to learn go ahead and make an implementation, but be aware that it won't be production ready anytime soon. For example copying data using std::memcpy is very limiting on what kind of data you are producing/consuming as most C++ types can't be just copied using std::memcpy. To know if it's safe to copy a type using std::memcpy you can use https://en.cppreference.com/w/cpp/types/is_trivially_copyable

Mircea Ispas
  • 20,260
  • 32
  • 123
  • 211
  • Yes the memory is shared between a publisher process and multiple subscribers processes. The data is a char array which can be copied using memcpy. I'm not using locking since I only need to control the order of stores within a process, just that writing into memory is successful before another writing operation following it is re-ordered as they are at different addresses. – Manan Agarwal Jul 02 '21 at 14:20
  • @MananAgarwal if you have multi process then the term memory barrier is confusing. – Mircea Ispas Jul 02 '21 at 14:30
  • what I need is to ensure is that the the publisher first writes the message in shared memory segment and when this store in memory is complete then it's offset is stored in memory at different location. The code is as needed : first the publisher writes the message using memcpy() then stores it's offset in memory at different location. Since the stores within a process also can be reordered it might cause the offset to be updated first and then the message will be completely written. I need to tackle this issue. I read about memory barriers which can be used to control stores, so asked it – Manan Agarwal Jul 02 '21 at 15:03