Thread: pthread reader/write locks question

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    35

    pthread reader/write locks question

    I'm just messing around with mutex and rwlock in pthreads. I wrote a quick little sample. but what's interesting is that the rwlock behaves much differently.

    Code:
    #include <stdlib.h>
    #include <stdbool.h>
    #include <pthread.h>
    #include <stdio.h>
    #include <string.h>
    #include <time.h>
    #include <unistd.h>
    
    int kill = 0;
    char * s = NULL;
    bool userw = false;
    pthread_t tim;
    pthread_t th1;
    pthread_t th2;
    pthread_mutex_t klock = PTHREAD_MUTEX_INITIALIZER;
    pthread_mutex_t slock = PTHREAD_MUTEX_INITIALIZER;
    pthread_rwlock_t slock2 = PTHREAD_RWLOCK_INITIALIZER;
    
    void * thfn1(void * args) {
    	printf("thfn1\n");
    	bool ext = false;
    	while(1) {
    		pthread_mutex_lock(&klock);
    		if(kill == 1) ext = true;
    		pthread_mutex_unlock(&klock);
    		if(ext == true) break;
    		if(userw) pthread_rwlock_wrlock(&slock2);
    		else pthread_mutex_lock(&slock);
    		if(s) free(s);
    		s = strdup("bbb");
    		printf("%s\n",s);
    		sleep(arc4random() % 5);
    		printf("th1 awake\n");
    		if(userw) pthread_rwlock_unlock(&slock2);
    		else pthread_mutex_unlock(&slock);
    	}
    	printf("thread1 finished\n");
    	return NULL;
    }
    
    void * thfn2(void * args) {
    	printf("thfn2\n");
    	bool ext = false;
    	while(1) {
    		pthread_mutex_lock(&klock);
    		if(kill == 1) ext = true;
    		pthread_mutex_unlock(&klock);
    		if(ext == true) break;
    		if(userw) pthread_rwlock_wrlock(&slock2);
    		else pthread_mutex_lock(&slock);
    		if(s) free(s);
    		s = strdup("ccc");
    		printf("%s\n",s);
    		sleep(arc4random() % 5);
    		printf("th2 awake\n");
    		if(userw) pthread_rwlock_unlock(&slock2);
    		else pthread_mutex_unlock(&slock);
    	}
    	printf("thread 2 finished\n");
    	return NULL;
    }
    
    void * timfn(void * args) {
    	int activetime = *(int *)args;
    	sleep(activetime);
    	pthread_mutex_lock(&klock);
    	kill = 1;
    	pthread_mutex_unlock(&klock);
    	return NULL;
    }
    
    int main(int argc, char ** argv) {
    	int activetime = 45; //seconds
    	pthread_create(&th1,NULL,thfn1,NULL);
    	pthread_create(&th2,NULL,thfn2,NULL);
    	pthread_create(&tim,NULL,timfn,&activetime);
    	pthread_join(th1,NULL);
    	pthread_join(th2,NULL);
    	pthread_join(tim,NULL);
    	return 0;
    }
    When this is run with read/write locks on, only the first thread ever acquires the write lock. But with read/write lock turned off, the threads will alternate access to the "s" variable. Note that it's not meant to be a 1to1 alternation.

    Any ideas why the second thread never acquires the write lock?

    Thanks!

  2. #2
    Registered User
    Join Date
    Jul 2009
    Posts
    35
    nvmnd, I think everything was working as expected, I updated the print statements to help be more identifiable and everything is working as I thought it would...

    Code:
    #include <stdlib.h>
    #include <stdbool.h>
    #include <pthread.h>
    #include <stdio.h>
    #include <string.h>
    #include <time.h>
    #include <unistd.h>
    
    int kill = 0;
    bool userw = true;
    pthread_t tim;
    pthread_t th1;
    pthread_t th2;
    pthread_mutex_t klock = PTHREAD_MUTEX_INITIALIZER;
    pthread_mutex_t slock = PTHREAD_MUTEX_INITIALIZER;
    pthread_rwlock_t slock2 = PTHREAD_RWLOCK_INITIALIZER;
    
    bool shouldkill() {
    	bool ret = false;
    	pthread_mutex_lock(&klock);
    	if(kill == 1) ret = true;
    	pthread_mutex_unlock(&klock);
    	return ret;
    }
    
    void * thfn2(void * args) {
    	printf("entered thfn2\n");
    	int res = 0;
    	while(1) {
    		if(shouldkill())break;
    		printf("th2: lock\n");
    		if(userw) res = pthread_rwlock_wrlock(&slock2);
    		else res = pthread_mutex_lock(&slock);
    		printf("th2: locked\n");
    		sleep(arc4random() % 5);
    		if(userw) res = pthread_rwlock_unlock(&slock2);
    		else res = pthread_mutex_unlock(&slock);
    		printf("th2: unlocked\n");
    	}
    	printf("thread2 finished\n");
    	return NULL;
    }
    
    void * thfn3(void * args) {
    	printf("entered thfn3\n");
    	int res = 0;
    	while(1) {
    		if(shouldkill())break;
    		printf("th3: lock\n");
    		if(userw) res = pthread_rwlock_wrlock(&slock2);
    		else res = pthread_mutex_lock(&slock);
    		printf("th3: locked\n");
    		sleep(arc4random() % 5);
    		if(userw) pthread_rwlock_unlock(&slock2);
    		else pthread_mutex_unlock(&slock);
    		printf("th3 unlocked\n");
    	}
    	printf("thread3 finished\n");
    	return NULL;
    }
    
    void * timfn(void * args) {
    	int activetime = *(int *)args;
    	sleep(activetime);
    	pthread_mutex_lock(&klock);
    	kill = 1;
    	pthread_mutex_unlock(&klock);
    	return NULL;
    }
    
    int main(int argc, char ** argv) {
    	int activetime = 15; //seconds
    	printf("th1 entered\n");
    	pthread_create(&th1,NULL,thfn2,NULL);
    	pthread_create(&th2,NULL,thfn3,NULL);
    	pthread_create(&tim,NULL,timfn,&activetime);
    	pthread_join(th1,NULL);
    	pthread_join(th2,NULL);
    	pthread_join(tim,NULL);
    	printf("th1 finished\n");
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick pthread question - starting paused/suspended
    By NightWolf8800 in forum C Programming
    Replies: 3
    Last Post: 10-24-2009, 08:15 AM
  2. Pthread question
    By gundamz2001 in forum C Programming
    Replies: 3
    Last Post: 09-16-2009, 04:04 AM
  3. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  4. pthread question
    By rotis23 in forum Linux Programming
    Replies: 10
    Last Post: 04-07-2004, 02:57 AM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM