5

用固定的 boost::wregex 声明一个静态/全局变量是否安全,然后从多个线程使用它而不用担心正则表达式的内部状态(如果 Boost 已使用 BOOST_HAS_THREADS 编译)?

例如

boost::wregex g_regex( L"common|test" );

然后有多个线程调用:

if ( boost::regex_search( test_str, g_regex ) )
...
4

1 回答 1

4

http://www.boost.org/doc/libs/1_51_0/libs/regex/doc/html/boost_regex/background_information/thread_safety.html

类 basic_regex 及其类型定义 regex 和 wregex 是线程安全的,因为编译后的正则表达式可以在线程之间安全地共享。匹配算法 regex_match、regex_search 和 regex_replace 都是可重入和线程安全的。类 match_results 现在是线程安全的,因为匹配的结果可以安全地从一个线程复制到另一个线程(例如,一个线程可能找到匹配项并将 match_results 实例推送到队列中,而另一个线程将它们从另一端弹出),否则为每个线程使用单独的 match_results 实例。

于 2012-09-04T12:01:40.907 回答