Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

boost

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 ) )
...
like image 382
snowdude Avatar asked Oct 07 '22 18:10

snowdude


1 Answers

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.

like image 89
ForEveR Avatar answered Oct 10 '22 02:10

ForEveR