Third-party libraries
Posted Aug 27, 2009 23:10 UTC (Thu) by
quotemstr (subscriber, #45331)
In reply to:
Third-party libraries by giraffedata
Parent article:
In brief
Or you just do this (error checks omitted):
#include <pthread.h>
static int fd = -1;
static pthread_mutex_t fd_lock = PTHREAD_MUTEX_INITIALIZER;
static void mylib_beforefork() {
pthread_mutex_lock(&fd_lock);
}
static void mylib_afterfork() {
pthread_mutex_unlock(&fd_lock);
}
void mylib_dosomething() {
pthread_mutex_lock(&fd_lock);
if(fd == -1) {
pthread_atfork(mylib_beforefork,
mylib_afterfork,
mylib_afterfork);
fd = open(...);
fcntl(fd, F_SETFD, O_CLOEXEC);
}
pthread_mutex_unlock(&fd_lock);
do_something(fd);
}
(
Log in to post comments)