#include #include #include #define RW_LOCK_BIAS 0x00100000 #define WRITE_LOCK_CMP RW_LOCK_BIAS /** Example implementation of linux rw lock along with 2 thread test * driver... */ typedef union { std::atomic lock; } rwlock_t; static inline int write_trylock(rwlock_t *rw) { int priorvalue = atomic_fetch_sub(&rw->lock, RW_LOCK_BIAS); if (priorvalue == RW_LOCK_BIAS) return 1; atomic_fetch_add(&rw->lock, RW_LOCK_BIAS); return 0; } static inline void write_unlock(rwlock_t *rw) { atomic_fetch_add(&rw->lock, RW_LOCK_BIAS); } rwlock_t mylock; int shareddata; void * a(void *obj) { problemsize if (write_trylock(&mylock)) {write_unlock(&mylock);} return NULL; } int main(int argc, char **argv) { pthread_t t1, t2; mylock.lock = RW_LOCK_BIAS; pthread_create(&t1, 0, &a, NULL); pthread_create(&t2, 0, &a, NULL); pthread_join(t1, 0); pthread_join(t2, 0); return 0; }