Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is this library multithreaded safe? #20

Open
skyformat99 opened this issue Jun 20, 2020 · 3 comments
Open

Is this library multithreaded safe? #20

skyformat99 opened this issue Jun 20, 2020 · 3 comments

Comments

@skyformat99
Copy link

Is this library multithreaded safe?

@chuckwolber
Copy link

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
Copy link
Author

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

@chuckwolber
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants