Is the get_a()
function safe for race-conditions or do I need to explicitly copy str_
as in get_b()
in order to have a thread-safe function?
class Class {
public:
auto get_a() -> std::string {
auto&& guard = std::lock_guard{mutex_};
return str_;
}
auto get_b() -> std::string {
auto&& guard = std::lock_guard{mutex_};
auto str = str_;
return str;
}
private:
std::mutex mutex_{};
std::string str_{};
};
Note: I'm aware that there are similar questions here on Stack Overflow, but I could not find one which explicitly answers this question.