C++ if one thread writes toggles a bool once done, is it safe to read that bool in a loop in a single other thread?

纵饮孤独 提交于 2021-02-16 08:57:15

问题


I am building a very simple program as an exercise.

The idea is to compute the total size of a directory by recursively iterating over all its contents, and summing the sizes of all files contained in the directory (and its subdirectories).

To show to a user that the program is still working, this computation is performed on another thread, while the main thread prints a dot . once every second.

Now the main thread of course needs to know when it should stop printing dots and can look up a result. It is possible to use e.g. a std::atomic<bool> done(false); and pass this to the thread that will perform the computation, which will set it to true once it is finished. But I am wondering if in this simple case (one thread writes once completed, one thread reads periodically until nonzero) it is necessary to use atomic data types for this. Obviously if multiple threads might write to it, it needs to be protected. But in this case, there's only one writing thread and one reading thread.

Is it necessary to use an atomic data type here, or is it overkill and could a normal data type be used instead?


回答1:


Yes it is necessary. The rule is that if two threads could potentially be accessing the same memory at the same time, and at least one of the threads is a writer, then you have a data race. Any execution of a program with a data race has undefined behavior.

Relevant quotes from the C++14 standard:

1.10/23

The execution of a program contains a data race if it contains two potentially concurrent conflicting actions, at least one of which is not atomic, and neither happens before the other, except for the special case for signal handlers described below. Any such data race results in undefined behavior.

1.10/6

Two expression evaluations conflict if one of them modifies a memory location (1.7) and the other one accesses or modifies the same memory location.




回答2:


Yes, it's necessary.

The issue is that the different cores of the processor can have different views of the "same" data, notably data that's been cached within the CPU. The atomic part ensures that these caches are properly flushed so that you can safely do what you are trying to do.

Otherwise, it's quite possible that the other thread will never actually see the flag change from the first thread.




回答3:


Yes, it is necessary. Otherwise it is not guaranteed that changes to the bool in one thread will be observable in the other thread. In fact, if the compiler sees that the bool variable is, apparently, not ever used again in the execution thread that sets it, it might completely optimize away the code that sets the value of the bool.



来源:https://stackoverflow.com/questions/41212494/c-if-one-thread-writes-toggles-a-bool-once-done-is-it-safe-to-read-that-bool

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!