I created an instance of "boost::interprocess::managed_shared_memory" and constructed an array of char with "2 * 1024 * 1024 * 1024" elements. Unfortunately it took time more than 50 seconds.
namespace bip = boost::interprocess;
auto id_ = "shmTest"s;
size_t size_ = 2*1024*1024*1024ul;
auto ashmObj_ = make_unique<bip::managed_shared_memory>(bip::create_only,
id_.c_str(),
size_ );
auto data_ = shmObj_->construct<char>("Data")[size_]('\0');
After that I got rid of it's initializing and decrease time to 30 second.
auto data_ = shmObj_->construct<char>("Data")[size_]();
Is there any way to get better time for this operation?
Sidenote: I don't think the size calculation expression is safe for the reason you seem to think (ul): https://cppinsights.io/s/c34003a4
The code as given should always fail with
bad_allocbecause you didn't account for the segment manager overhead:Fixing it e.g. like this runs in 5s for me:
Changing to
makes no significant difference:
If you want opaque char arrays, just could just use a mapped region directly:
Now it's significantly faster:
BONUS
If you insist you can do raw allocation from the segment:
Or, you can just use the segment as it intended, and let is manage your allocations:
This takes a little more time:
But for that you get enormous flexibility. Just search some of my existing posts for examples using complicated data structures in managed shared memory: https://stackoverflow.com/search?tab=newest&q=user%3a85371%20scoped_allocator_adaptor