关于C的重入

Paw*_*ari 2 c reentrancy

如果您有一个库中的函数f_func()并且您知道它不可重入,那么您将如何在线程环境(POSIX)中使用它?您无法访问库的源代码.

Cra*_*tey 6

您可以将其包装在互斥锁中.以下是一个示例用法:

pthread_mutex_t f_func_mutex = PTHREAD_MUTEX_INITIALIZER;

pthread_mutex_lock(&f_func_mutex);

f_func();

// if f_func has "side effects", such as setting a global, you'll want to grab
// the value within the locked region:
int local = global_set_by_f_func;

pthread_mutex_unlock(&f_func_mutex);
Run Code Online (Sandbox Code Playgroud)