anthozep anthozep - 1 year ago 65
Linux Question

PCRE pcre_exec thread safe?

I have a C program that uses a PCRE regex to determine if a process in a cgroup should be added to one variable or another. I spawn a thread to read the cpuacct.stat file in each running cgroup, where the number of threads never exceeded the number of cores. These samples and results are then combined into one of two variables.

The relevant snippet of code is:

pcreExecRet = pcre_exec(reCompiled,
strlen(queue), // length of string
0, // Start looking at this point
30); // Length of subStrVec

pthread_mutex_lock(&t_lock); //lock mutex
while (sumFlag == 0) {
pthread_cond_wait(&ok_add, &t_lock); //wait on ok signal

if(pcreExecRet > 0) {
sumOne += loadavg;
} else if (pcreExecRet == PCRE_ERROR_NOMATCH){
sumTwo += loadavg;
} else {
perror("Could not determine sum!\n"); //if this fails


sumFlag = 1;

pthread_cond_signal(&ok_add); //signal that it is ok to add
pthread_mutex_unlock(&t_lock); //unlock mutex

My question is whether or not the pcre_exec() call is thread-safe? Should it be moved into the critical section? I know the compiled regex is thread safe, but I'm not sure about pcreExtra (const pcre_extra) or subStrVec (int *ovector). These variables are global for now.

Answer Source

Yes it is thread safe, all PCRE functions are but you should be careful under certain conditions

The following is from the manual pages for PCRE


   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  match-
   ing, 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  sepa-
   rate  memory stack areas for each thread. See the pcrejit documentation
   for more details.