Pawan Adhikari Pawan Adhikari - 1 year ago 98
C Question

Regarding reentrancy in C

If you have a function from a library f_func() and you know it's not re-entrant. How would you use it in a threaded environment (POSIX)? You can't access the source code of the library.

Answer Source

You can wrap it in a mutex. Here is an example usage:

pthread_mutex_t f_func_mutex = PTHREAD_MUTEX_INITIALIZER;



// 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;

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download