How do I use an arbitrary string as a lock in C++?

前端 未结 6 1890
半阙折子戏
半阙折子戏 2021-02-04 15:15

Let\'s say I have a multithreaded C++ program that handles requests in the form of a function call to handleRequest(string key). Each call to handleRequest

6条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-04 15:51

    Raise granularity and lock entire key-ranges

    This is a variation on Mike B's answer, where instead of having several fluid lock maps you have a single fixed array of locks that apply to key-ranges instead of single keys.

    Simplified example: create array of 256 locks at startup, then use first byte of key to determine index of lock to be acquired (i.e. all keys starting with 'k' will be guarded by locks[107]).

    To sustain optimal throughput you should analyze distribution of keys and contention rate. The benefits of this approach are zero dynamic allocations and simple cleanup; you also avoid two-step locking. The downside is potential contention peaks if key distribution becomes skewed over time.

提交回复
热议问题