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?
Terence
___________________________________________________________________________ #include <stdio.h> rwlock_t RWLock; void* writer(); void main() g = 0; rwlock_init ( &RWLock, USYNC_THREAD, 0 ); thrResult1 = thr_create(NULL,0,writer,NULL,THR_NEW_LWP|THR_BOUND|THR_SUSPENDED,&tid1); thr_continue(tid1); for (;;); int i,k,cnt=0; for ( ;; ) { for ( ;; ) {
#include <thread.h>
#include <synch.h>
#include <errno.h>
long g;
void* reader();
{
thread_t tid1,tid2;
int thrResult1,thrResult2;
int pri;
thrResult2 = thr_create(NULL,0,reader,NULL,THR_NEW_LWP|THR_BOUND|THR_SUSPENDED,&tid2);
thr_continue(tid2);
void* writer()
{
for ( ; rw_trywrlock( &RWLock ) != 0; ) {};
g++;
if ( cnt++ > 1000000 ) {
cnt = 0;
};
rw_unlock ( &RWLock );
thr_yield();
};
void* reader()
{
int cnt;
rw_rdlock ( &RWLock );
if ( cnt++ > 1000000 ) {
cnt = 0;
printf("Reader 1 : %d \n",g);
};
rw_unlock ( &RWLock );
thr_yield();
};