Is a static boost::wregex instance thread-safe?

2019-06-22 10:12发布

问题:

Is it safe to declare a static/global variable with a fixed boost::wregex and then use it from multiple threads without worrying about the regex's internal state (if Boost has been compiled with BOOST_HAS_THREADS)?

e.g.

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

then have multiple threads calling:

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

回答1:

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

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