简体   繁体   English

std::regex 线程安全吗?

[英]Is std::regex thread safe?

Related to Is a static boost::wregex instance thread-safe?静态 boost::wregex 实例线程安全吗? but for the standarized version.但对于标准化版本。 Can I call regex_search from several threads with the same regex object?我可以从具有相同正则表达式对象的多个线程调用 regex_search 吗?

Claiming that std::regex is thread-safe in every respect is a pretty bold statement.声称std::regex在各个方面都是线程安全的,这是一个非常大胆的声明。 The C++11 standard does not make such guarantees for the regex library. C++11 标准不对正则表达式库做出此类保证。

However, looking at the prototype of std::regex_search shows that it it takes the basic_regex object as a const argument.但是,查看std::regex_search的原型表明它将basic_regex对象作为 const 参数。 This means that it is protected by the standard library's guarantee that the const modifier implies thread-safety of the function with respect to that argument.这意味着它受到标准库保证的保护,即 const 修饰符暗示函数相对于该参数的线程安全

In standardese, that is:用标准话来说,就是:

[17.6.5.9/1] This section specifies requirements that implementations shall meet to prevent data races (1.10). [17.6.5.9/1]本节规定了实现应满足的要求,以防止数据竞争 (1.10)。 Every standard library function shall meet each requirement unless otherwise specified.除非另有说明,否则每个标准库函数都应满足每个要求。 Implementations may prevent data races in cases other than those specified below.实施可能会在下面指定的情况以外的情况下防止数据竞争。

[17.6.5.9/3] A C++ standard library function shall not directly or indirectly modify objects (1.10) accessible by threads other than the current thread unless the objects are accessed directly or indirectly via the function's non-const arguments, including this . [17.6.5.9/3] C++ 标准库函数不得直接或间接修改可由当前线程以外的线程访问的对象 (1.10),除非通过函数的非 const参数直接或间接访问对象,包括this

So, barring a bug in the implementation of the standard library that you use, it appears that calls to std::regex_search are thread-safe with respect to the regex object that is passed in.因此,除非您使用的标准库的实现中出现错误,否则对std::regex_search的调用似乎对于传入的regex对象是线程安全的。


Other thoughts:其他想法:

Just because std::regex_search is re-entrant with respect to its regex argument does not mean that you are completely out of the water.仅仅因为std::regex_search就其regex参数而言是可重入的,并不意味着您完全不在水中。 Performing an operation that modifies a regex in a non-thread-safe manner at the same time as a thread-safe call such as std::regex_search is still undefined behaviour.std::regex_search等线程安全调用的同时执行以非线程安全方式修改regex的操作仍然是未定义的行为。 basic_regex 's assignment operator , std::swap , and basic_regex::imbue come to mind as non-thread-safe functions with respect to the basic_regex they operate on. basic_regex赋值运算符std::swapbasic_regex::imbue相对于它们操作的basic_regex而言是非线程安全函数。 Knowing this, it might be better for you to make a copy of the regex object, which should come at a minimal performance cost, for each thread to use/modify at its leisure.知道了这一点,您最好制作一个regex对象的副本,这应该以最低的性能成本为每个线程在闲暇时使用/修改。

While Sean's answer is true of the standard, individual implementation may fall short.虽然Sean 的回答对标准是正确的,但个别实施可能会达不到要求。 VC++ 2013, at least, looks like it has race conditions in its copy constructor and in a lazily evaluated variable.至少,VC++ 2013 看起来在其复制构造函数和惰性求值变量中具有竞争条件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM