我有一个C程序,它使用PCRE正则表达式来确定cgroup中的进程是否应该添加到一个变量或另一个变量中。我生成一个线程来读取每个正在运行的cgroup中的cpuacct.stat文件,其中线程数量从未超过核心数量。这些样本和结果然后被组合成两个变量之一。PCRE pcre_exec线程安全吗?
的代码中的相关片段是:
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_exec()'说?此外,您可以使用[* helgrind *](http://www.valgrind.org/info/tools.html#helgrind)检查与线程相关的问题。 –
因为我认为显而易见的原因,Ovector不可共享线程。我会为每个线程分配一个。 – rici