I am learning pthread and came across a writer lock. The script is very simple; a global variable shared by all threads, the reader continues to print the current value of the same global variable, while the writer updates the same variable. I can achieve this synchronization using two mutexes (pthread_mutex_t), but I want to use a “single” read-write lock to achieve the same result. However, with a single reader-writer lock, as seen here (program output below), the reader sees only the first x value and does not see any updates to the global variable. Please throw some light here.
code:
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <pthread.h>
#include <poll.h>
#define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
int x = 0;
pthread_rwlock_t lock_rw = PTHREAD_RWLOCK_INITIALIZER;
void *reader_thread(void *arg)
{
int i;
int newx, oldx;
newx = oldx = -1;
pthread_rwlock_t *p = (pthread_rwlock_t *)arg;
if (pthread_rwlock_rdlock(p) != 0) {
perror("reader_thread: pthread_rwlock_rdlock error");
exit(__LINE__);
}
for (i = 0; i < 100; i++) {
newx = ACCESS_ONCE(x);
if (newx != oldx) {
printf("reader_lock: x: %d\n",x);
}
oldx = newx;
poll(NULL, 0, 1);
}
if (pthread_rwlock_unlock(p) != 0) {
perror("reader thread: pthred_rwlock_unlock error");
exit(__LINE__);
}
return NULL;
}
void *writer_thread(void *arg)
{
int i;
pthread_rwlock_t *p = (pthread_rwlock_t *)arg;
if (pthread_rwlock_wrlock(p) != 0) {
perror("writer thread: pthread_rwlock_wrlock error");
exit(__LINE__);
}
for (i = 0; i < 3; i++) {
ACCESS_ONCE(x)++;
poll(NULL, 0, 5);
}
if (pthread_rwlock_unlock(p) != 0) {
perror("writer thread: pthread_rwlock_unlock error");
exit(__LINE__);
}
return NULL;
}
int main(void)
{
pthread_t tid1, tid2;
void *vp;
if (pthread_create(&tid1, NULL, reader_thread, &lock_rw) != 0) {
perror("pthread_create error");
exit (__LINE__);
}
if (pthread_create(&tid2, NULL, writer_thread, &lock_rw) != 0) {
perror("pthread_create error");
exit (__LINE__);
}
if (pthread_join(tid1, &vp) != 0) {
perror("pthread_join error");
exit (__LINE__);
}
if (pthread_join(tid2, &vp) != 0) {
perror("pthread_join error");
exit (__LINE__);
}
printf("Parent process sees x: %d\n",x);
return 0;
}
gcc pthread_rwlock.c -o rwlock -pthread -Wall -Werror
./rwlock
reader_lock: x: 0
x: 3