Map in Shared memory

后端 未结 1 1926
难免孤独
难免孤独 2021-01-12 11:45

I am trying to create an unordered_map in shared memory. I am using allocator to server the purpose.

The code

void *addr;
void *pool;
int shmid;

tem         


        
1条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-12 12:10

    As I said last time you asked about this, you can't store pointers in shared memory and expect them to be usable in more than one process. As before, I suggest using boost::unordered_map in conjunction with boost::interprocess::allocator.

    If you really want to write your own allocator, you'll need some kind of custom pointer that stores the offset into the shared memory block, and is able to reconstruct the correct address from that. I wouldn't recommend trying to do this yourself: as well as it being quite fiddly, you may find that some implementations of std::unordered_map don't work correctly with unconventional pointer types. In fact, I suspect that it might be impossible to make a standard-compliant allocator for shared memory; otherwise, the Boost allocator would surely be usable by all standard containers, not just special Boost versions.

    By the way, you shouldn't use reserved names like __p (containing a double underscore) in your own code; they should only be used by the Standard Library implementation.

    0 讨论(0)
提交回复
热议问题