Is this library multithreaded safe?
skyformat99 opened this issue · comments
skyformat99 commented
Is this library multithreaded safe?
Chuck Wolber commented
If you use proper locking, then yes it would be. Here is an example.
#include <pthread.h>
#include "log.h"
pthread_mutex_t MUTEX_LOG;
void log_lock(bool lock, void *udata);
int main() {
pthread_mutex_init(&MUTEX_LOG, NULL);
log_set_lock(log_lock, &MUTEX_LOG);
/* Insert threaded application code here... */
pthread_mutex_destroy(&MUTEX_LOG);
return 0;
}
void log_lock(bool lock, void* udata) {
pthread_mutex_t *LOCK = (pthread_mutex_t*)(udata);
if (lock)
pthread_mutex_lock(LOCK);
else
pthread_mutex_unlock(LOCK);
}
skyformat99 commented
thanks.
It would be nice to add this code directly to log.c
Chuck Wolber commented
Possibly, but locking is an extraordinarily complicated topic, and there is no one-size-fits all approach that would work with a portable library like this. For example, the above example only works in POSIX style operating systems, like Linux, BSD, and macOS, that support NPTL.