May volatile be in user defined types to help writing thread-safe code

前端 未结 8 2230
别那么骄傲
别那么骄傲 2020-12-04 18:19

I know, it has been made quite clear in a couple of questions/answers before, that volatile is related to the visible state of the c++ memory model and not to m

8条回答
  •  悲&欢浪女
    2020-12-04 19:15

    I think the issue is not about thread-safety provided by volatile. It dosen't and Andrei's article dosen't say it does. Here, a mutex is used to achieve that. The issue is, whether the use of volatilekeyword to provide static type-checking along with use of mutex for thread-safe code, is abuse of the volatile keyword? IMHO it's pretty smart, but i have come across developers who are not fans of strict-type-checking just for the sake of it.

    IMO when you are writing code for multi-threaded environment, there is already enough caution to emphasize wherein you would expect people not to be ignorant of race-conditions and deadlocks.

    A downside of this wrapped approach is that every operation on the type that is wrapped using LockingPtr must be through a member function. That will increase one level of indirection which might considerably affect developers comfort in a team.

    But if you are a purist who believes in the spirit of C++ a.k.a strict-type-checking; this is a good alternative.

提交回复
热议问题