Do I need to protect this variable with a lock?

后端 未结 6 1248
天涯浪人
天涯浪人 2021-02-15 11:58

so I have a boolean type in C++ on a multiprocessor machine. The variable starts out life as true, and then there are a few threads, any one or more of which might write it to b

6条回答
  •  一生所求
    2021-02-15 12:36

    For a bool, the answer is generally no, a mutex is not needed, but (as Michael E noted) anything could happen, so you likely need to understand more about your arch before making such a decision. Another note: the code might still need a lock around the overall logic associated with the bool, especially if the bool is read more than once in the course of a routine's logic.

    Some great blogs I read to keep me on my multithreaded toes:

    http://blogs.msdn.com/b/nativeconcurrency/

    http://herbsutter.com/2009/04/20/effective-concurrency-use-thread-pools-correctly-keep-tasks-short-and-nonblocking/

    best regards,

提交回复
热议问题