misc/24641: pthread_rwlock_rdlock can deadlock

Earl Chew earl_chew at agilent.com
Mon Jan 5 14:30:25 PST 2004


The following reply was made to PR misc/24641; it has been noted by GNATS.

From: Earl Chew <earl_chew at agilent.com>
To: aspiesrule at mcleodusa.net
Cc: freebsd-gnats-submit at freebsd.org
Subject: Re: misc/24641: pthread_rwlock_rdlock can deadlock
Date: Mon, 05 Jan 2004 14:21:00 -0800

 aspiesrule at mcleodusa.net wrote:
 > Same results, except for the fact that stepping thru your code in GDB yields 
 > a segfault in KERNEL32!IsBadWritePtr.
 
 It must be the holidays! Ok, I've added the missing line at the
 beginning of wrfunc().
 
 Earl
 --
 #include <pthread.h>
 #include <stdio.h>
 #include <unistd.h>
 #include <assert.h>
 
 static pthread_rwlock_t rwlock1 = PTHREAD_RWLOCK_INITIALIZER;
 
 static volatile int wrStarted;
 
 void * wrfunc(void *unused)
 {
    wrStarted = 1;
 
    printf("Attempt to acquire write lock\n");
    assert(pthread_rwlock_wrlock(&rwlock1) == 0);
    printf("Acquired write lock\n");
 
    assert(pthread_rwlock_unlock(&rwlock1) == 0);
 
    return 0;
 }
 
 static volatile int rdStarted;
 
 void * rdfunc(void *unused)
 {
    printf("Attempt to acquire read lock first\n");
    assert(pthread_rwlock_rdlock(&rwlock1) == 0);
    printf("Acquired read lock first\n");
 
    rdStarted = 1;
 
    while (wrStarted == 0)
        sleep(1);
 
    printf("Attempt to acquire read lock second\n");
    assert(pthread_rwlock_rdlock(&rwlock1) == 0);
    printf("Acquired read lock second\n");
 
    assert(pthread_rwlock_unlock(&rwlock1) == 0);
    assert(pthread_rwlock_unlock(&rwlock1) == 0);
 
    return 0;
 }
 
 int
 main(int argc, char **argv)
 {
    pthread_t wrt;
    pthread_t rdt;
    pthread_attr_t a;
 
    assert(pthread_rwlock_init(&rwlock1, 0) == 0);
 
    assert(pthread_attr_init(&a) == 0);
 
    assert(pthread_create(&rdt, &a, rdfunc, NULL) == 0);
 
    while (rdStarted == 0)
        sleep(1);
 
    assert(pthread_create(&wrt, &a, wrfunc, NULL) == 0);
 
    assert(pthread_join(wrt, 0) == 0);
    assert(pthread_join(rdt, 0) == 0);
 
    assert(pthread_rwlock_destroy(&rwlock1) == 0);
 
    assert(pthread_detach(wrt) == 0);
    assert(pthread_detach(rdt) == 0);
 
    return 0;
 }
 


More information about the freebsd-threads mailing list