用固定的 boost::wregex 声明一个静态/全局变量然后在多个线程中使用它而不用担心正则表达式的内部状态(如果 Boost 是用 BOOST_HAS_THREADS 编译的)是否安全?
例如
boost::wregex g_regex( L"common|test" );
然后有多个线程调用:
if ( boost::regex_search( test_str, g_regex ) )
...
最佳答案
Class basic_regex and its typedefs regex and wregex are thread safe, in that compiled regular expressions can safely be shared between threads. The matching algorithms regex_match, regex_search, and regex_replace are all re-entrant and thread safe. Class match_results is now thread safe, in that the results of a match can be safely copied from one thread to another (for example one thread may find matches and push match_results instances onto a queue, while another thread pops them off the other end), otherwise use a separate instance of match_results per thread.
关于c++ - 静态 boost::wregex 实例是线程安全的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12263013/