Здесь вы найдете простое и портативное решение, основанное на семафорах posix:
http://stupefydeveloper.blogspot.com/2009/03/nix-rw-lock-with-semaphores.html
#include <semaphore.h>
#include <errno.h>
struct rwlock {
sem_t rlock;
sem_t wlock;
};
void init_rwlock(struct rwlock *rw) {
sem_init(&rw->rlock, 1, 0);
sem_init(&rw->wlock, 1, 1);
}
void destroy_rwlock(struct rwlock *rw) {
wlock(rw);
sem_destroy(&rw->rlock);
sem_destroy(&rw->wlock);
}
void rlock(struct rwlock *rw){
sem_wait(&rw->wlock);
sem_post(&rw->rlock);
sem_post(&rw->wlock);
}
void wlock(struct rwlock *rw) {
int readers = 0;
sem_wait(&rw->wlock);
do {
if (sem_getvalue(&rw->rlock, &readers) != -1) {
if (readers == 0)
break;
}
usleep(1);
} while (1);
}
void runlock(struct rwlock *rw) {
do {
if (sem_trywait(&rw->rlock) == -1) {
if (errno != EAGAIN)
continue;
}
break;
} while (1);
}
void wunlock(struct rwlock *rw) {
sem_post(&rw->wlock);
}