Multithread programming on Read/Write Lock

Multithread programming on Read/Write Lock

Post by Teren » Sun, 19 Feb 1995 22:47:23

In response to my previoud posted problem on rw_lock in multi-threading program,
I discovered that by using thr_yield after completion of one unit of work for
both Writer and Reader would prevent the seeminly deadlock situation.  
Can this approach solve th problem ?

However, I suspect that context switching on threads will be significant.
Is it true for this case?



#include <stdio.h>
#include <thread.h>
#include <synch.h>
#include <errno.h>

rwlock_t RWLock;
long g;

void* writer();
void* reader();

void main()
        thread_t tid1,tid2;
        int thrResult1,thrResult2;
        int pri;

        g = 0;  

        rwlock_init ( &RWLock, USYNC_THREAD, 0 );

        thrResult1 = thr_create(NULL,0,writer,NULL,THR_NEW_LWP|THR_BOUND|THR_SUSPENDED,&tid1);
        thrResult2 = thr_create(NULL,0,reader,NULL,THR_NEW_LWP|THR_BOUND|THR_SUSPENDED,&tid2);


        for (;;);


void* writer()

        int i,k,cnt=0;

        for  ( ;; ) {
            for ( ; rw_trywrlock( &RWLock ) != 0; ) {};                                    
            if  ( cnt++ > 1000000 ) {
                cnt = 0;
            rw_unlock ( &RWLock );


void* reader()
        int cnt;

        for ( ;; ) {
            rw_rdlock ( &RWLock );
            if  ( cnt++ > 1000000 ) {
                cnt = 0;
                printf("Reader 1 : %d  \n",g);
            rw_unlock ( &RWLock );



Multithread programming on Read/Write Lock

Post by Bil Lew » Mon, 20 Feb 1995 12:30:25


  Did you see my reply?  (I'm not sure it showed up here.)  Basically I see
no particular problems (a few nits) with your program, and it works as
expected for me.

Det ?r den som g?r vilse, som finner de nya v?garna

Developer Technical Evangelism


1. Reader lock-free read/write lock

I have a new algorithm in the rcpc package here
Acually, three algorithms, one using LL/SC, one
using double wide compare and swap, and one using
single wide compare and swap though it's only
really safe in 64 bit mode.  The last is good if
you happen to be on an architecture that doesn't
support the first two techniques.  In theory it
could be ported to 32 bit sparc using CASX as a
double wide compare and swap and 64 bit sparc using
CASX as single wide compare and swap.  I say in
theory since I don't have my SB100 anymore.

Joe Seigh

When you get lemons, you make lemonade.
When you get hardware, you make software.

2. kernel upgrades docs ?

3. Question on writing c program implement cp function using UNIX system calls - read, write, etc.

4. really stoopit question...

5. Need help writing C program using UNIX system calls (read, write, etc) that copies files

6. expectk (tcl) problem on RedHat (?)

7. how to read disk read & write i/o through a c/c++ program ?

8. Machine Check Exception

9. Sockets: write() writes OK, yet read() reads garbage...?

10. Read-Write locks

11. furwocks: Fast Userspace Read/Write Locks

12. Read/Write Locks

13. File Read/Write locking ...