PCRE pcre_exec thread safe?

孤人 提交于 2021-02-10 06:13:05

问题


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,
                        pcreExtra,
                        queue,
                        strlen(queue),          // length of string
                        0,                      // Start looking at this point
                        0,                      // OPTIONS
                        subStrVec,
                        30);                    // Length of subStrVec

    //CRITICAL SECTION?
    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.


回答1:


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

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  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.


来源:https://stackoverflow.com/questions/38928867/pcre-pcre-exec-thread-safe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!