2

Is it possible to use std::string or other complex structure key type with boost unordered map on shared memory? If so, can I find some sample code? I found some sample code for using unordered map on shared memory but were unable to change the key type to complex structure. Thanks

Tim3880
  • 2,563
  • 1
  • 11
  • 14
  • Could you elaborate what you mean with "shared memory"? Are you running a multi-threaded application and want to share a map between the threads? If so, how do you achieve the multithreading (pthreads, std::thread, boost::thread, tbb, OpenMP ...)? – Walter May 05 '15 at 16:41
  • Basically we need a big "Map" mapping ip addresses to user info (a structure) and we are currently doing it with boost::unordered_map. It's working fine using char[] as key, in one process. We need move the Map to shared memory so multiple processes can "look up" in the shared Map. We need some sample code to help this move. – Tim3880 May 05 '15 at 17:04

1 Answers1

0

Yes.

You can see an example here:

You will want to watch the overhead when doing lookups. Using non-standard key equality/hash functions can solve this:

Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633
  • Thanks for the sample links. However, I was unable to compile the first one, it gives me an error expected primary-expression before ‘{’ token on the line ComplexType(std::string, Alloc2 const& alloc = {}) : map(alloc) {}. My compiler might be too old: gcc version 4.1.2 20080704 (Red Hat 4.1.2-55) – Tim3880 May 05 '15 at 20:33
  • Your compiler is too old. Just use c++03 equivalents (`= Alloc2()` here). Anyhoops: **[Live On Coliru](http://coliru.stacked-crooked.com/a/a6d02ba11c5520d4)** – sehe May 05 '15 at 21:09