Is the PCRE library thread-safe? If not, which do

2019-09-03 16:18发布

问题:

For C/C++, is the PCRE library thread-safe? If PCRE is thread-safe, is there any problem in performance?

回答1:

Judging from the PCRE documentation:

MULTITHREADING

The PCRE functions can be used in multi-threading applications, with the proviso that the memory management functions pointed to by pcre_malloc, pcre_free, pcre_stack_malloc, and pcre_stack_free, and the callout and stack-checking functions pointed to by pcre_callout and pcre_stack_guard, are shared by all threads.

The compiled form of a regular expression is not altered during matching, so the same compiled pattern can safely be used by several threads at once.

If the just-in-time optimization feature is being used, it needs separate memory stack areas for each thread. See the pcrejit documentation for more details.

All of this means that if you are careful, the answer is 'Yes, the PCRE library is thread-safe'.



回答2:

Little off-topic, but I recommend to have a look at RE2 (RE2 is thread-friendly)