misc/24641: pthread_rwlock_rdlock can deadlock
Earl Chew
earl_chew at agilent.com
Mon Jan 5 11:20:35 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: The Thodes <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 11:11:40 -0800
The Thodes wrote:
> First, Earl, GCC objected to your test program.
Apologies. I compiled this as a C++ program (g++) hence the missing
parameter names.
> The patch given is needed to make it compile (in my case, at least).
Yes, your patch will fill in the "junk" names, and allow the C
compiler to parse the source successfully.
> Also, it (your test program)
> doesn't even get to trying to acquire the second read lock. It hangs
> trying to acquire the write lock. The output is provided in this (long)
> follow-up.
Oops... there was a cut-and-paste error. Please look at the amended
program below.
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)
{
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