C++11: why does std::condition_variable use std::unique_lock?

后端 未结 2 1753
梦毁少年i
梦毁少年i 2020-12-04 10:06

I am a bit confused about the role of std::unique_lock when working with std::condition_variable. As far as I understood the documentation, s

2条回答
  •  无人及你
    2020-12-04 10:28

    It's essentially an API design decision to make the API as safe as possible by default (with the additional overhead being seen as negligible). By requiring to pass a unique_lock instead of a raw mutex users of the API are directed towards writing correct code (in the presence of exceptions).

    In recent years the focus of the C++ language has shifted towards making it safe by default (but still allowing users to shoot themselves into their feet if they want to and try hard enough).

提交回复
热议问题