log.c icon indicating copy to clipboard operation
log.c copied to clipboard

Is this library multithreaded safe?

Open skyformat99 opened this issue 5 years ago • 3 comments

Is this library multithreaded safe?

skyformat99 avatar Jun 20 '20 11:06 skyformat99

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);
}

chuckwolber avatar Aug 08 '20 02:08 chuckwolber

thanks. It would be nice to add this code directly to log.c

skyformat99 avatar Aug 08 '20 02:08 skyformat99

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.

chuckwolber avatar Aug 08 '20 02:08 chuckwolber