Main Page   Packages   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Search  

C:/temp/src/j2k/Deprecated/old_18mar_diff/nto/RWLock_Test.cpp

Go to the documentation of this file.
00001 #include <j2k/Fred/Standard.hpp>
00002 #include <j2k/nto/Basic_PThread.hpp>
00003 #include <j2k/nto/Mutex.hpp>
00004 #include <j2k/nto/RWLock.hpp>
00005 
00006 #define MAX  100000
00007 
00008 RWLock r;
00009 long p = 100;
00010 
00011 class Fred : public Basic_PThread 
00012 {
00013 public:
00014    Fred( char* n = ".", char c1 = '*', char c2 = '*' ) : Basic_PThread(), 
00015    s( n ), k1( c1 ), k2( c2 ) { } 
00016 
00017    virtual void run() {
00018    for(;;) {
00019            printf(",");
00020            r.write();
00021            p += s[0];
00022            printf( "%c", k1 );
00023            printf( "Write:%s", s );
00024            printf( "%c", k2 );
00025            printf(",");
00026            sched_yield();
00027            r.releaseWrite();
00028    }
00029    } 
00030 
00031    char* s;
00032    char  k1;
00033    char  k2;
00034 };
00035 
00036 class Reader : public Basic_PThread 
00037 {
00038 public:
00039    Reader( char* n = ".", char c1 = '*', char c2 = '*' ) : Basic_PThread(), 
00040    s( n ), k1( c1 ), k2( c2 ) { } 
00041 
00042    virtual void run() {
00043    for(;;) {
00044            printf(",");
00045            r.read();
00046            printf( "%c", k1 );
00047            printf( "Read:%s", s );
00048            printf( "%c", k2 );
00049            printf(",");
00050            sched_yield();
00051            r.releaseRead();
00052    }
00053    } 
00054 
00055    char* s;
00056    char  k1;
00057    char  k2;
00058 };
00059 
00060 
00061 int main( int argc = 0, char* argv[] = NULL) 
00062 {
00063 
00064   // Verify that the Critical Section inside the [CS] (CS) {CS} respectively
00065   // is not interrupted. 
00066   // i.e. Simple Mutual exclusion test.
00067 
00068   // Example: if you see [( ]) or [()] or ([{]}) or anything alike then the code is not working.
00069   // Perhaps, if you see [](){} or anyother combination, then it works just fine!
00070 
00071   Fred* t1 = new Fred( "0",  '[', ']' );
00072   Fred* t2 = new Fred( "1",  '(', ')' );
00073   Fred* t3 = new Fred( "2",  '{', '}' );
00074 
00075   Reader* r1 = new Reader( "3",  '[', ']' );
00076   Reader* r2 = new Reader( "4",  '(', ')' );
00077   Reader* r3 = new Reader( "5",  '{', '}' );
00078 
00079   t1->start();
00080   t2->start();
00081   t3->start();
00082 
00083   r1->start();
00084   r2->start();
00085   r3->start();
00086 
00087   printf("t1-2-3 started.\n\n");
00088 
00089   int max = MAX;
00090 
00091   if ( argc > 1 ) 
00092   {
00093     printf("max = %s, %d", argv[1], argc );
00094     max = atoi( argv[1] );
00095     if ( max < 10 ) max = MAX;
00096   } 
00097 
00098   for( int i = 0; i < max; i++ ) { 
00099    printf( "." );
00100 
00101 //   sched_yield();
00102   }
00103 
00104   return 0;
00105 }

Generated on Sun Oct 14 18:46:24 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001