1

I was hoping if someone could forward me or show me a program that has multiple readers yet mutually excluding writers in C. I searched the entire internet for it, and could not find a single example that displays this behavior using coarse-grained locking. I know I can use pthread_rwlock_init, pthread_rwlock_rdlock, etc, I just don know how to use it. I learn by examples, which is why Im here.

Suppose I have a region of code(not a shared variable)and I want multiple reads, yet a single writer, this is what I don't know how to accompolish using pthreads rwlocks. I don't understand how the code will know that now it is being written to, compared to now it is being read. Thanks.

Ozymandy
  • 59
  • 1
  • 7

1 Answers1

1

You can take a look at page 24 of Peter Chapin's Pthread Tutorial for an example. I added it below.

#include<pthread.h>
int shared;
pthread_rwlock_t lock ;
void∗ thread_function (void∗ arg)
{
  pthread_rwlock_rdlock (&lock);
  //Read from the shared resource.
  pthread_rwlock_unlock(&lock);
}
void main( void )
{
  pthread_rwlock_init(&lock, NULL);
  // Start threads here.
  pthread_rwlock_wrlock(& lock );
  // Write to the shared resource .
  pthread_rwlock_unlock(&lock);
  // Join_with threads here .
  pthread_rwlock_destroy(&lock);
  return 0 ;
}
Ryan Ahearn
  • 7,886
  • 7
  • 51
  • 56
Paul Rubel
  • 26,632
  • 7
  • 60
  • 80
  • so now, I can read twice, and what happens if I want to write, and the reading is not yet done? Does it wait for the read to finish and then it writes? Also, after you have the write lock, don't you need to unlock the write lock, that way we can make room for reading in the future? – Ozymandy Apr 03 '13 at 18:00
  • @user2241639 Its a read/write lock. So long as there are at leat one reader(s) holding the lock, no writer can take it. Likewise so long as a writer has the lock, no reader nor writer can take the lock. And your next question, yes, the possibility of thread-starvation for a given writer waiting for a large number of readers to relinquish the lock is *very* real. – WhozCraig Apr 03 '13 at 18:12
  • @WhozCraig so if one is reading, and the writer wants to write, is he in a queue, or does the actual attempt of writing fail? – Ozymandy Apr 03 '13 at 18:22
  • @user2241639 the writer blocks until all readers and/or another single writer releases the lock. For a writer to acquire a write-lock, *no one* can have *any* lock. For a reader to acquire a read-lock, no writer can have a write-lock. I hope that is clear. – WhozCraig Apr 03 '13 at 18:31
  • The writer would fail to acquire the lock, blocking. He wouldn't get a chance to write. Note that there are also try versions of the calls that will attempt to lock but returns a value saying whether or not the lock was acquired. You could use this to attempt to get the lock, but not block if it was unavailable. – Paul Rubel Apr 03 '13 at 19:02
  • what "binary_sem" in "pthread_wlock_destroy (&binary_sem);" ? – Vladimir Poslavskiy Nov 25 '13 at 12:02
  • Looks like it was a bug. I got a new copy from the updated version and made a working link. – Paul Rubel Nov 25 '13 at 13:25