我有一个 C 程序,它使用 PCRE 正则表达式来确定 cgroup 中的进程是否应添加到一个变量或另一个变量。我生成一个线程来读取每个正在运行的 cgroup 中的 cpuacct.stat 文件,其中线程数从未超过核心数。然后将这些样本和结果组合成两个变量之一。
相关代码片段是:
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
我的问题是 pcre_exec() 调用是否是线程安全的?是否应该将其移至临界区?我知道编译的正则表达式是线程安全的,但我不确定 pcreExtra (const pcre_extra) 或 subStrVec (int *ovector)。这些变量目前是全局的。
是的,它是线程安全的,全部PCRE功能是,但在某些情况下你应该小心
以下内容来自于手册页PCRE http://www.pcre.org/pcre.txt
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.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)