Massive CPU load using std::lock (c++11)

前端 未结 4 990
梦毁少年i
梦毁少年i 2020-12-07 15:09

My recent efforts to implement a thread/ mutex manager ended up in an 75% CPU load (4 core), while all four running threads were either in sleep or waiting for a mutex beein

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-07 15:47

    The std::lock() non-member function may cause Live-lock problem or performance degradation, it guarantee only "Never Dead-lock".

    If you can determine "Lock order(Lock hierarchy)" of multiple mutexes by design, it's preferable to not use generic std::lock() but lock each mutexes in pre-determinate order.

    Refer to Acquiring Multiple Locks Without Deadlock for more detail.

提交回复
热议问题