同时使用std :: regex,定义行为? [英] Concurrently using std::regex, defined behaviour?

查看:138
本文介绍了同时使用std :: regex,定义行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以在多个线程中使用 std :: regex 对象所有使用 std :: sregex_iterator std :: regex_match 等等。


例如,会产生以下逻辑行为:

  bool SomeFunc(const std :: string& szString1,const std :: string& szString2)
{
static const std :: regex regexTest(===([\\w] +)===,std :: regex_constants :: optimize);

std :: future< bool> f = std :: async([](std :: string szString){
return std :: regex_match(szString,regexTest);
},szString1);

bool b = std :: regex_match(szString2,regexTest);

return(b&& f.get());
}



我找不到任何说明是否使用 const std :: regex 同时导致未定义的行为或不行。就我所知,没有对正则表达式对象进行编辑,所以没有未定义的行为应该通过并发使用诱导?



提前感谢!

解决方案

是的,一个 const std :: regex 是线程安全的。实际上,标准库中的任何const方法都是线程安全的,参见:


§17.6.5.9/ 3。 C ++标准库函数不应直接或
间接修改由
当前线程以外的线程访问的对象(1.10),除非对象直接或间接访问
通过函数的非const参数,包括此。



Is it possible to use a std::regex object in multiple threads all using std::sregex_iterator, std::regex_match, etc.?

For instance, would the following produce logical behaviour:

bool SomeFunc( const std::string& szString1, const std::string& szString2 ) 
{
     static const std::regex regexTest( "=== ([\\w]+) ===", std::regex_constants::optimize );

     std::future<bool> f = std::async( []( std::string szString ) {
        return std::regex_match( szString, regexTest );  
     }, szString1 );

     bool b = std::regex_match( szString2, regexTest );

     return (b && f.get());
}

I can't find anything which states whether using a const std::regex concurrently results in undefined behaviour or not. As far as I can tell, no edits are being made to the regex object so no undefined behaviour should be induced by using it concurrently?

Thanks in advance!

解决方案

Yes, a const std::regex is thread-safe. Actually any const method in the standard library is thread-safe, see:

§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.

这篇关于同时使用std :: regex,定义行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆