Is it possible to implement lock free map in C++

后端 未结 6 915
再見小時候
再見小時候 2020-12-02 23:33

We are developing a network application based C/S, we find there are too many locks adding to std::map that the performance of server became poor.

I wonder if it is

相关标签:
6条回答
  • 2020-12-03 00:10

    If you use C++11, you can have a look at AtomicHashMap of facebook/folly

    0 讨论(0)
  • 2020-12-03 00:10

    I'm surprised nobody has mentioned it, but Click Cliff has implemented a wait-free hashmap in Java, which I believe could be ported to C++,

    0 讨论(0)
  • 2020-12-03 00:11

    Actually there's a way, although I haven't implemented it myself there's a paper on a lock free map using hazard pointers from eminent C++ expert Andrei Alexandrescu.

    0 讨论(0)
  • 2020-12-03 00:18

    HashMap would suit? Have a look at Intel Threading Building Blocks, they have an interesting concurrent map. I'm not sure it's lock-free, but hopefully you're interested in good multithreading performance, not particularly in lock-freeness. Also you can check CityHash lib

    EDIT:

    Actually TBB's hash map is not lock-free

    0 讨论(0)
  • 2020-12-03 00:27

    Yes, I have implemented a Lock-Free Unordered Map (docs) in C++ using the "Split-Ordered Lists" concept. It's an auto-expanding container and supports millions of elements on a 64-bit CAS without ABA issues. Performance-wise, it's a beast (see page 5). It's been extensively tested with millions of random ops.

    0 讨论(0)
  • 2020-12-03 00:27

    You can implement the map using optimistic design or transactional memory.

    This approach is especially effective if the chance of two operations concurrently addressing the map and one is changing the structure of it is relatively small - and you do not want the overhead of locking every time.

    However, from time to time - collision will occur, and you will have to result it somehow (usually by rolling back to the last stable state and retrying the operations).

    If your hardware support good enough atomic operations - this can be easily done with Compare And Swap (CAS) - where you change the reference alone (and whenever you change the map, you work on a copy of the map, and not the original, and set it as the primary only when you commit).

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