使用固定的 boost::wregex 声明静态/全局变量,然后从多个线程使用它,而不用担心正则表达式的内部状态(如果 Boost 已使用 BOOST_HAS_THREADS 进行编译)是否安全?
e.g.
boost::wregex g_regex( L"common|test" );
然后有多个线程调用:
if ( boost::regex_search( test_str, g_regex ) )
...
http://www.boost.org/doc/libs/1_51_0/libs/regex/doc/html/boost_regex/background_information/thread_safety.html http://www.boost.org/doc/libs/1_51_0/libs/regex/doc/html/boost_regex/background_information/thread_safety.html
basic_regex 类及其 typedef regex 和 wregex 是线程安全的,因为已编译的正则表达式可以在线程之间安全地共享。匹配算法 regex_match、regex_search 和 regex_replace 都是可重入且线程安全的。match_results 类现在是线程安全的,因为匹配结果可以安全地从一个线程复制到另一个线程(例如,一个线程可能找到匹配项并将 match_results 实例推送到队列中,而另一个线程将它们从另一端弹出),否则,每个线程使用单独的 match_results 实例。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)