Related to Is a static boost::wregex instance thread-safe? but for the standarized version. Can I call regex_search from several threads with the same regex object?

解决方案

Claiming that std::regex is thread-safe in every respect is a pretty bold statement. The C++11 standard does not make such guarantees for the regex library.

However, looking at the prototype of std::regex_search shows that it it takes the basic_regex object as a const argument. 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.

In standardese, that is:

[17.6.5.9/1]

This section specifies requirements that implementations shall meet to prevent data races (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.

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.

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

std string与线程安全,是std :: regex线程安全吗?相关推荐

  1. 关于std::string 在 并发场景下 __grow_by_and_replace free was not allocated 的异常问题

    使用string时发现了一些坑. 我们知道stl 容器并不是线程安全的,所以在使用它们的过程中往往需要一些同步机制来保证并发场景下的同步更新. 应该踩的坑还是一个不拉的踩了进去,所以还是记录一下吧. ...

  2. C++/C++11中std::string用法汇总

    C++/C++11中std::string是个模板类,它是一个标准库.使用string类型必须首先包含<string>头文件.作为标准库的一部分,string定义在命名空间std中. st ...

  3. ATL::CStringA和std::string之间转换的一些误区

    对于刚做windows下VC的开发同学,类型转换应该是一个令其很苦恼的问题.我刚写工作的时候,也为这类问题不停的在网上搜索转换方法.最近工作中遇到一个"神奇"的bug(一般&quo ...

  4. std::string的find问题研究

    https://files-cdn.cnblogs.com/files/aquester/std之string的find问题研究.pdf 目录 目录 1 1. 前言 1 2. find字符串 1 3. ...

  5. 将整个ASCII文件读入C ++ std :: string [重复]

    本文翻译自:Read whole ASCII file into C++ std::string [duplicate] This question already has an answer her ...

  6. 如何将std :: string转换为const char *或char *?

    如何将<code>std::string转换为char*或const char* ? #1楼 看看这个: string str1("stackoverflow"); c ...

  7. C++ 标准程序库std::string 详解

    现在一般不再使用传统的char*而选用C++标准程序库中的string类,是因为string标准程序和char*比较起来,不必担心内存是否足够.字符串长度等等,而且作为一个类出现,集成的操作函数足以完 ...

  8. ncnn报无法将参数 1 从“std::string”转换为“const ncnn::DataReader

    详细描述 无法将参数 1 从"std::string"转换为"const ncnn::DataReader",下面代码报错 std::string param_ ...

  9. std::string::substr

    std::string::substr Defined in header <string> - 定义于头文件 <string> public member function ...

  10. std::string int 转换

    c++     std::string test("11);     int startid = std::stoi(test);      注意:test 为空,则会抛出异常     st ...

最新文章

  1. NSNotification消息通知实现源码(观察者模式原理)
  2. / /* /**的区别
  3. Java并发编程之:Vector和ArrayList的区别
  4. ETL异构数据源Datax_使用querySql_08
  5. LeetCode 532. K-diff Pairs in an Array
  6. 【华为云技术分享】如何度量前端项目研发效率与质量(上)
  7. python计算相关性显著性p值_基于python实现计算两组数据P值
  8. oracle报ora-08002,ORA-08002: 序列 SEQ_WGB_TEST2.CURRVAL 尚未在此会话中定义
  9. react-native 列表组件的简单学习
  10. 【SAR综述】复杂场景单通道SAR目标检测与鉴别方法
  11. 文档翻译——免费版(word格式、pdf格式)
  12. APS Interview - Principles of Computer Organization
  13. 浏览器点击后退提示_当点击浏览器后退时会发生什么
  14. 蚌埠学院计算机操作系统题库,蚌埠学院电子教务登录
  15. Java3d获取坐标_java-使用带有xzyz坐标和jzy3d的3d表面图
  16. 云上城之个服务器维护时间,云上城之歌开服时间表 官方最新开服情况
  17. integrate函数python_python – Sympy:integrate()奇怪的输出
  18. todolist从无到有
  19. manjaro和android数据线传数据
  20. 谈谈MySQL那些事

热门文章

  1. Flask中的HttpResponse Redirect 和Render
  2. mysql备份时过滤掉某些库 以及 去掉Warning: Using a password on the command line interface can be insecure.提示信息...
  3. [改善Java代码]三元操作符的类型务必一致
  4. php输出echo、print、print_r、printf、sprintf、var_dump比较
  5. 常见的Activity Action Intent常量
  6. [Linux] - xxx 不在 sudoers 文件中。此事将被报告。
  7. 读《编程之美》读后感3
  8. java接口的作用是什么?
  9. 基于应用层自身反远程线程注入的研究
  10. Mr.J-- jQuery学习笔记(七)--CSS类操作文本值操作