本文介绍了根据 C++11(语言/库)标准,线程安全函数的定义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL;DR:说特定函数是线程安全的"是什么意思,因为同时调用两个可能不同的发生数据争用 功能?这个问题在人们告诉const 意味着/暗示 C++11 中的线程安全"的上下文中尤其相关.[1][2]

TL;DR: What is meant by saying a specific function is 'thread-safe' as a data race occurs by simultaneously calling two possibly different functions? This question is especially relevant in the context of people telling "const means/implies thread-safe in C++11" [1][2]

考虑以下示例:

class X {
    int x, y; // are some more complex type (not supported by `std::atomic`)
    std::mutex m;
public:
    void set_x (int new_x) {x = new_x;} // no mutex
    void get_x () const {return x;}

    void set_y (int new_y) {
        std::lock_guard<std::mutex> guard(m); // guard setter with mutex
        y = new_y;
    }
    void get_y () const {return y;}
}

set_x 线程安全吗?

Is set_x thread safe?

当然,set_x 不是线程安全的,因为同时从两个线程调用它会导致数据竞争.

Off course, set_x is not thread safe as calling it from two threads simultaneously results in a data race.

get_xget_yset_y 线程安全吗?

Are get_x, get_y and set_y thread safe?

存在两种可能的原因:

  1. 是的,它们是线程安全的,因为同时从两个线程调用 get_x/get_y/set_y 不会导致数据竞争.
  2. 不,它们不是线程安全的,因为调用 get_x(或 get_y)和 set_x(或 set_y>) 同时来自两个线程会导致数据竞争.
  1. Yes, they are thread safe, as calling get_x/get_y/set_y from two threads simultaneously does not result in a data race.
  2. No, they are not thread safe, as calling get_x (or get_y) and set_x (or set_y) from two threads simultaneously results in a data race.

对于这三个功能中的每一个,哪个是正确的推理?

Which one is the correct reasoning for each of those three functions?

哪个推理是正确的?

  1. 一个函数是线程安全的,如果同时从两个线程调用它不会导致数据竞争.可以对 set_x/get_x 起作用,但对 set_y/get_y 无效,因为这会导致以下结论set_yget_y 是线程安全的,但是 Y 类不是调用 set_yget_y 同时来自两个线程会导致数据竞争.
  2. 一个函数是线程安全的,如果它不访问任何可以被另一个函数在没有内部同步的情况下修改的内存.在我看来,这似乎是最一致的选项,但不是经常使用的方式(请参阅相关主题).
  1. A function is thread safe iff calling it from two threads simultaneously does not result in a data race. Could work for set_x/get_x, but fails for set_y/get_y, as this would result to the conclusion that set_y and get_y are thread safe, but class Y isn't as calling set_y and get_y from two threads simultaneously results in a data race.
  2. A function is thread safe iff it does not access any memory that could be modified without internal synchronization by another function. This seems to me the most consistent option, but is not the way it is often used (see related threads).


相关主题

请注意,我已阅读以下相关主题:


Related threads

Note that I have read the following related threads:

  • Does const mean thread-safe in C++11? ['mean' = it's your duty to make it]
  • How do I make a function thread safe in C++?
  • https://isocpp.org/blog/2012/12/you-dont-know-const-and-mutable-herb-sutter
  • https://softwareengineering.stackexchange.com/questions/379516/is-the-meaning-of-const-still-thread-safe-in-c11

推荐答案

请注意,这是基于我自己的研究并提供其他人的意见的我自己的观点.

一个函数是线程安全的,如果它不访问(读或写)任何可以被另一个函数在没有内部同步的情况下修改的内存:只有 set_y 是线程安全的.

A function is thread safe iff it does not access (read or write) any memory that could be modified by another function without internal synchronization: only set_y is thread-safe.

请注意,C++ 标准并未明确定义线程安全,该标准使用术语数据竞争.有关更多信息,请参阅 Nicol Bolas 的回答:线程安全并不总是非黑即白.

Note that thread-safe is not explicitly defined by the C++ standard, which uses the term data races. See the answer of Nicol Bolas for more information: thread-safety is not always black and white.

术语线程安全const 函数意味着线程安全"的上下文中被滥用.

The term thread-safe is abused in the context of "a const function implies thread-safe".

const 函数意味着线程安全"的意思是从多个线程调用 const 函数应该是安全的(调用非常量函数同时在另一个线程中).

What is meant by "a const function implies thread-safe", is that it should be safe to call the const function from multiple threads (without calling non-const function at the same time in another thread).

作为 Herb Sutter (29:43) 自己说,在这种情况下,线程安全意味着按位常量 内部同步,如果其他非常量,这不是真正的线程安全函数可以同时调用.

As Herb Sutter (29:43) stated himself, in this context, thread-safe means bitwise const or internally synchronised, which isn't really thread-safe if other non-const functions may be called at the same time.

这篇关于根据 C++11(语言/库)标准,线程安全函数的定义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 06:39