object-design

std::mutex vs std::recursive_mutex as class member

别来无恙 提交于 2019-12-03 00:53:12
问题 I have seen some people hate on recursive_mutex : http://www.zaval.org/resources/library/butenhof1.html But when thinking about how to implement a class that is thread safe (mutex protected), it seems to me excruciatingly hard to prove that every method that should be mutex protected is mutex protected and that mutex is locked at most once. So for object oriented design, should std::recursive_mutex be default and std::mutex considered as an performance optimization in general case unless it

std::mutex vs std::recursive_mutex as class member

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 14:17:30
I have seen some people hate on recursive_mutex : http://www.zaval.org/resources/library/butenhof1.html But when thinking about how to implement a class that is thread safe (mutex protected), it seems to me excruciatingly hard to prove that every method that should be mutex protected is mutex protected and that mutex is locked at most once. So for object oriented design, should std::recursive_mutex be default and std::mutex considered as an performance optimization in general case unless it is used only in one place (to protect only one resource)? To make things clear, I'm talking about one