0

I am writing a Boost.Interprocess code for shared memory between two processes. I am using managed_shared_memory. I'm initializing the memory using boost::interprocess::open_or_create, and I am allocating memory in it using find_or_construct, like this:

auto data_ptr = segment.find_or_construct<Data>("data")[max_elements]();

I am using find_or_construct so that one process initializes the memory and another process receives the data structure. All of this is atomic.

I want to ensure that data_ptr is aligned to a cache line, meaning it must start at an address that is a multiple of 64 bytes (on my system). How can I achieve this alignment?

Additional Information: The system cache line size is 64 bytes. I'm using Boost.Interprocess for shared memory management.

I looked into Boost.Interprocess documentation but couldn't find a direct way to specify memory alignment for find_or_construct. It will be really helpful if I can get some other way to align memory with Boost, and ensure one process initializes the memory while another process receives the data structure, without any synchronization issues.

3
  • It max_elements a compile-time constant? Commented Jul 14, 2024 at 15:17
  • yes, max_elements is a compile time constant Commented Jul 14, 2024 at 15:29
  • 1
    Then you can write a type struct DataArray { alignas(64) Data data[max_elements]; }; and then use auto data_ptr = segment.find_or_construct<DataArray>("data")().data;, assuming that the library handles overaligned types correctly (which I hope it would, but don't know). Commented Jul 14, 2024 at 16:30

1 Answer 1

0

Note that this has been fixed in 1.87.0.

segment_manager nows uses a new implementation that supports overaligned types and fixes some existing bugs. You can obtain the pre-Boost 1.87 ABI #defining BOOST_INTERPROCESS_SEGMENT_MANAGER_ABI to 1 before including Boost.Interprocess headers.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.