这里确实有两个不同的问题:我可以在多线程程序中使用正则表达式而不锁定吗?如果可以,我可以在多个线程中同时使用相同的 regex_t 吗?我在 Google 或联机帮助页上找不到答案。
最佳答案
http://www.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html
2.9.1 Thread-Safety
All functions defined by this volume of POSIX.1-2008 shall be thread-safe, except that the following functions1 need not be thread-safe.
...
regexec
和 regcomp
不在该列表中,因此它们必须是线程安全的。
另请参阅:http://www.opengroup.org/onlinepubs/9699919799/functions/regcomp.html
部分理由文本如下:
The interface is defined so that the matched substrings rm_sp and rm_ep are in a separate regmatch_t structure instead of in regex_t. This allows a single compiled RE to be used simultaneously in several contexts; in main() and a signal handler, perhaps, or in multiple threads of lightweight processes.
关于c - posix regcomp 和 regexec 线程安全吗?具体来说,在 GNU libc 上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4404574/