ࡱ> t&( </ 0DTimes New RomanԲ 0 0DLucida ConsoleԲ 0 01  @n?" dd@  @@`` @'k      c $@g4FdFd 0x ppp@  <4BdBd g44d4d 0x Vp@ pp<4!d!dL? %O =#Multi-theaded programming with NSPR Larry HardimanOverviewWhy program using threads? What you need to program with threads NSPR s thread abstraction Thread synchronization using locks Thread synchronization using condition variables Techniques for high performance threadsAWhy program using theads?}Simplify program design Improve performance when calling blocking functions Utilize all processors on multi-processor systems Thread Programming Basics<Thread Management Thread private data Thread synchronization"+= Thread Management^PR_CreateThread() PR_Interrupt() PR_JoinThread() PR_SetThreadPriority() PR_GetThreadPriority()P   Creating a thread1 #include  prthread.h 2 PRThread * 3 PR_CreateThread( 4 PRThreadType type, /* user or system */ 5 (void)(start)(void *arg), /* function */ 6 void *arg; /*argument to start function */ 7 PRThreadPriority priority, /* low, norm & */ 8 PRThreadScope scope, /* local, global */ 9 PRThreadState state, /* joinable? */ 10 PRInt32 stacksize );kk    5'" !    Thread Private Data\Think of  thread private data as global variables, but for exclusive use for a single thread. Void (*PR_ThreadPrivateDTOR)(void*); PRStatus PR_NewThreadPrivateIndex( int *ndx, (dtor)); PRStatus PR_SetThreadPrivate( ndx, *priv ); void* PR_GetThreadPrivate( ndx ); See: mozilla/nsprpub/pr/tests/tpd.c"/^j     Thread Sync using PRLock21 #include  prlock.h 2 PRLock *lock; 3 lock = PR_NewLock(); 4 PR_Lock(lock); 5 /* operate on your data & */ 6 PR_Unlock(lock); 7 PR_DestroyLock(lock);P L Using Condition Variables1 PRIntn data = 0; /* Thread A */ 2 PRLock *lock = PR_NewLock(); 3 PRCondVar *cvar = PR_NewCondVar(lock); 4 PR_Lock(lock); 5 while( data == 0 ) 6 PR_WaitCondVar( cvar, timeout ); /* lock released while blocked. */ /* re-locked at fall through */ 7 data = 0; /* satisfy Thread B s condition */ 8 PR_NotifyCondVar( cvar ); 9 PR_Unlock( lock );ff    6   Using Condition Variables/* Thread B */ 4 PR_Lock(lock); 5 while( data == 1 ) 6 PR_WaitCondVar( cvar, timeout ); /* lock released while blocked. */ /* re-locked at fall through */ 7 data = 1; 8 PR_NotifyCondVar( cvar ); 9 PR_Unlock( lock ); P= i !What Not to Do1 PRLock *lock; 2 PRCondVar *cv; 3 PR_Lock( lock ); 4 PR_WaitCondVar( cv, PR_INTERVAL_NO_TIMEOUT ); /* implies state in *cv */ 5 /* do something */ 6 PR_Unlock( lock ); There is no state in a PRCondVar..!  6H "*& More What Not to Dou1 PRBool dataIsAvailable = PR_FALSE; 2 PR_Lock( lock ); 3 if ( !dataIsAvailable ) 4 PR_WaitCondVar( cv, PR_INTERVAL_NO_TIMEOUT ); /* Bad! dataIsAvailable may change during wait */ 5 /* work on the available data */ 6 PR_Unlock( lock ); The lock is released during PR_WaitCondvar(). Other threads can change dataIsAvailable. Use the while() loop; test, wait, test again."v) #s 0Condvar Example A hacked cvar.c goes here.  High Performance Threads.& you are protecting data, not code partition assets so that you protect the smallest piece possible hold exclusive control as short a time as possible  BibliographyFDavid R. Butenhof, "Programming with POSIX Threads", Addison-Wesley, 1997 Steve Kleiman, et.al,  Programming with Threads , Prentice Hall, 1996 NSPR group,  NSPR Reference , Ch. 1, 3, 5, 6. http://www.mozilla.org/docs/refList/refNSPR/contents.html UseNet newsgroup: comp.programming.threads.L$: ?V &/       #$u ` ̙33` ` ff3333f` 333MMM` f` f` 3>?" dd@,|?" dd@   " @ ` n?" dd@   @@``PR    @ ` ` p>> c(    6$( P  T Click to edit Master title style! !  0(   RClick to edit Master text styles Second level Third level Fourth level Fifth level!     S  0( ``  =*  0D) `   ?*  0) `   ?*H  0޽h ? ̙330 0.(    0h% P    Y*   0i%     [* d  c $ ?    0ti%  @  RClick to edit Master text styles Second level Third level Fourth level Fifth level!     S  6i% `P   Y*   64j% `   [* H  0޽h ? ̙33 @(  l  C k%p  l  C l% `    H  0޽h ? ̙33  P ( w  l  C 4m%P  % l  C m% % H  0޽h ? ̙33  `( w l  C o%P  % l  C to% % H  0޽h ? ̙33   p$( w $l $ C 4p%P  % l $ C p% % H $ 0޽h ? ̙33   @( w @l @ C q%P  % l @ C r% % H @ 0޽h ? ̙33  (( `` (l ( C tr%P  % l ( C r% % H ( 0޽h ? ̙33  D(  Dl D C %P  % l D C 4% % H D 0޽h ? ̙33}   -%4( w 4l 4 C T%P   l 4 C %   4 <%& =,  1 H 4 0޽h ? ̙33  8( w 8l 8 C t%P  % l 8 C % % H 8 0޽h ? ̙33  T(  Tl T C %P  % l T C T% % H T 0޽h ? ̙33  ( w l  C %P  % l  C t% % H  0޽h ? ̙33   (  l  C g%P   l  C Th%  H  0޽h ? ̙33  d( w dl d C %P  % l d C 4% % H d 0޽h ? ̙33   <( w <l < C %P  % l < C % % H < 0޽h ? ̙33   \(  \l \ C %P  % l \ C t% % H \ 0޽h ? ̙330 x@ ( E  R  3    }~  C t% @  }  H  0޽h ? ̙33Z0 ,( ]mO ,R , 3    } , C t} @  } pType := system, user. & An artifact of when NSPR was the base for NS Java VM. & when all user threads end, the process terminates. & Create your threads as user threads. Scope := local, global, bound_global local = user thread, scheduled by NSPR global = user or kernel thread. Varies depending on platform. You do this. Global_bound = a kernel thread. State := joinable, not-joinable Priority = limited range: low, normal, high, urgent. ,k  6H , 0޽h ? ̙33 0 H(  HR H 3    } H C tu} @  } '  H H 0޽h ? ̙330 P&(  PR P 3    } P C u} @  % 4This is the ultra simple case. Thead A, Threa B & do a simple see-saw. No meaningful work. The while is necessary because the lock is released during the wait! Some platforms provide for spurious notifies. You gotta check the variable.>H P 0޽h ? ̙33 0 X( Ԡ XR X 3    % X C 4v} @  % '  H X 0޽h ? ̙330 xh( {̙ج hR h 3    ~ h C v} @    H h 0޽h ? ̙330 x0l( 0@ lR l 3    }~ l C % @  }  H l 0޽h ? ̙330 skPp( 0@ pR p 3    }q p C tl% @  } Compare threaded program to simple single thread program vs. state machine program When one thread blocks at a blocking function, other threads are running Multiple threads can execute simultaneously on multiple CPUs 9H p 0޽h ? ̙330 `t( i tR t 3    } t C s} @  } '  H t 0޽h ? ̙33 0 xpx( ̙33 xR x 3    }~ x C Tt} @  }  H x 0޽h ? ̙330 |(  |R | 3    } | C Tw} @  } '  H | 0޽h ? ̙33k 0 +# ( 0@ R  3    %)  C % @  % Protecting access to data, not a path through code Instead of locking a large aggregate, lock a piece avoid holding lock across a blocking call H  0޽h ? ̙33 0 x(  R  3    }~  C v} @  }  H  0޽h ? ̙330 x( |@@I@ R  3    }~  C tx} @  }  H  0޽h ? ̙330 x(   R  3    }~  C x} @  }  H  0޽h ? ̙33rP|&468.:Q?>lSADM_<@XdFZO}^LN`bcfjh2jlvnDH$J>pr s$$( </ 0DTimes New Roman 0 0DLucida Console 0 01  @n?" dd@  @@``Letter Paper (8.5x11 in)rap Netscapeper Times New RomanLucida ConsoleDefault Design%Multi-threaded programming with NSPR OverviewWhat is NSPR?Why program using threads?Thread Programming BasicsThread ManagementCreating a threadThread Private DataThread sync using PRLockUsing Condition VariablesUsing Condition VariablesWhat Not b/ 0DTimes New Roman|&|dv 0|( 0DLucida Console|&|dv 0|( 01 ` .  @n?" dd@  @@`` `+p"  DTimes New Roman 0 0DLucida Console 0 01  @n?" dd@  @@`` @'k       c $@g4FdFd 0x ppp@  <4BdBd g44d4d 0x Vp@ pp<4!d!dL? %O =$Multi-threaded programming with NSPR Larry HardimanOverviewWhat is NSPR? Why program using threads? What you need to program with threads NSPR s thread abstraction Thread synchronization using locks Thread synch using condition variables Techniques for high performance threadsOWhy program using threads?}Simplify program design Improve performance when calling blocking functions Utilize all processors on multi-processor systems Thread Programming Basics<Thread Management Thread private data Thread synchronization"+= Thread Management^PR_CreateThread() PR_Interrupt() PR_JoinThread() PR_SetThreadPriority() PR_GetThreadPriority()P   Creating a thread1 #include  prthread.h 2 PRThread * 3 PR_CreateThread( 4 PRThreadType type, /* user or system */ 5 (void)(start)(void *arg), /* function */ 6 void *arg; /*argument to start function */ 7 PRThreadPriority priority, /* low, norm & */ 8 PRThreadScope scope, /* local, global */ 9 PRThreadState state, /* joinable? */ 10 PRInt32 stacksize );kk    5'" !    Thread Private Data\Think of  thread private data as global variables, but for exclusive use for a single thread. Void (*PR_ThreadPrivateDTOR)(void*); PRStatus PR_NewThreadPrivateIndex( int *ndx, (dtor)); PRStatus PR_SetThreadPrivate( ndx, *priv ); void* PR_GetThreadPrivate( ndx ); See: mozilla/nsprpub/pr/tests/tpd.c"/^j     Thread Sync using PRLock21 #include  prlock.h 2 PRLock *lock; 3 lock = PR_NewLock(); 4 PR_Lock(lock); 5 /* operate on your data & */ 6 PR_Unlock(lock); 7 PR_DestroyLock(lock);P L Using Condition Variables1 PRIntn data = 0; /* Thread A */ 2 PRLock *lock = PR_NewLock(); 3 PRCondVar *cvar = PR_NewCondVar(lock); 4 PR_Lock(lock); 5 while( data == 0 ) 6 PR_WaitCondVar( cvar, timeout ); /* lock released while blocked. */ /* re-locked at fall through */ 7 data = 0; /* satisfy Thread B s condition */ 8 PR_NotifyCondVar( cvar ); 9 PR_Unlock( lock );ff    6   Using Condition Variables/* Thread B */ 4 PR_Lock(lock); 5 while( data == 1 ) 6 PR_WaitCondVar( cvar, timeout ); /* lock released while blocked. */ /* re-locked at fall through */ 7 data = 1; 8 PR_NotifyCondVar( cvar ); 9 PR_Unlock( lock ); P= i !What Not to Do1 PRLock *lock; 2 PRCondVar *cv; 3 PR_Lock( lock ); 4 PR_WaitCondVar( cv, PR_INTERVAL_NO_TIMEOUT ); /* implies state in *cv */ 5 /* do something */ 6 PR_Unlock( lock ); There is no state in a PRCondVar..!  6H "      c $@,g4CdCdv 0pppp@  <4BdBd 0,g4@d@dv 0p|p@ pp<4!d!d` 0,h'uʚ;2Nʚ;<4dddd{ 0:2___PPT9/ 0? %O =V  @'k     c $@g4FdFd 0x ppp@  <4BdBd g44d4d 0x Vp@ pp<4!d!dL? %O =$Multi-threaded programming with NSPR Larry HardimanOverviewWhy program using threads? What you need to program with threads NSPR s thread abstraction Thread synchronization using locks Thread synchronization using condition variables Techniques for high performance threadsAWhy program using theads?}Simplify program design Improve performance when calling blocking functions Utilize all processors on multi-processor systems Thread Programming Basics<Thread Management Thread private data Thread synchronization"+= Thread Management^PR_CreateThread() PR_Interrupt() PR_JoinThread() PR_SetThreadPriority() PR_GetThreadPriority()P   Creating a thread1 #include  prthread.h 2 PRThread * 3 PR_CreateThread( 4 PRThreadType type, /* user or system */ 5 (void)(start)(void *arg), /* function */ 6 void *arg; /*argument to start function */ 7 PRThreadPriority priority, /* low, norm & */ 8 PRThreadScope scope, /* local, global */ 9 PRThreadState state, /* joinable? */ 10 PRInt32 stacksize );$Multi-threaded programming with NSPR Larry HardimanOverviewWhat is NSPR? Why program using threads? What you need to program with threads NSPR s thread abstraction Thread synchronization using locks Thread synch using condition variables Techniques for high performance threadsO% What is NSPR?Original NSPR 1.0 was NS Java base NSPR 2.0 began in early 1996 NSPR 2 is platform abstraction No GUI Threading, thread sync File and Socket I/O Time, IPC, ...Why program using threads?}Simplify program design Improve performance when calling blockingDocumentSummaryInformation8kk    5'" !    Thread Private Data\Think of  thread private data as global variables, but for exclusive use for a single thread. Void (*PR_ThreadPrivateDTOR)(void*); PRStatus PR_NewThreadPrivateIndex( int *ndx, (dtor)); PRStatus PR_SetThreadPrivate( ndx, *priv ); void* PR_GetThreadPrivate( ndx ); See: mozilla/nsprpub/pr/tests/tpd.c"/^j     Thread Sync using PRLock21 #include  prlock.h 2 PRLock *lock; 3 lock = PR_NewLock(); 4 PR_Lock(lock); 5 /* operate on your data & */ 6 PR_Unlock(lock); 7 PR_DestroyLock(lock);P L Using Condition Variables1 PRIntn data = 0; /* Thread A */ 2 PRLock *lock = PR_NewLock(); 3 PRCondVar *cvar = PR_NewCondVar(lock); 4 PR_Lock(lock); 5 while( data == 0 ) 6 PR_WaitCondVar( cvar, timeout ); /* lock released while blocked. */ /* re-locked at fall through */ 7 data = 0; /* satisfy Thread B s condition */ 8 PR_NotifyCondVar( cvar ); 9 PR_Unlock( lock );ff    6   Using Condition Variables/* Thread B */ 4 PR_Lock(lock); 5 while( data == 1 ) 6 PR_WaitCondVar( cvar, timeout ); /* lock released while blocked. */ /* re-locked at fall through */ 7 data = 1; 8 PR_NotifyCondVar( cvar ); 9 PR_Unlock( lock ); P= i !What Not to Do1 PRLock *lock; 2 PRCondVar *cv; 3 PR_Lock( lock ); 4 PR_WaitCondVar( cv, PR_INTERVAL_NO_TIMEOUT ); /* implies state in *cv */ 5 /* do something */ 6 PR_Unlock( lock ); There is no state in a PRCondVar..!  6H "*& More What Not to Dou1 PRBool dataIsAvailable = PR_FALSE; 2 PR_Lock( lock ); 3 if ( !dataIsAvailable ) 4 PR_WaitCondVar( cv, PR_INTERVAL_NO_TIMEOUT ); /* Bad! dataIsAvailable may change during wait */ 5 /* work on the available data */ 6 PR_Unlock( lock ); The lock is released during PR_WaitCondvar(). Other threads can change dataIsAvailable. Use the while() loop; test, wait, test again."v) #s 0Condvar Example A hacked cvar.c goes here.  High Performance Threads.& you are protecting data, not code partition assets so that you protect the smallest piece possible hold exclusive control as short a time as possible  BibliographyFDavid R. Butenhof, "Programming with POSIX Threads", Addison-Wesley, 1997 Steve Kleiman, et.al,  Programming with Threads , Prentice Hall, 1996 NSPR group,  NSPR Reference , Ch. 1, 3, 5, 6. http://www.mozilla.org/docs/refList/refNSPR/contents.html UseNet newsgroup: comp.programming.threads.L$: ?V &/8 rtfty$:&( </ 0TNPP2OMi & TNPP &&TNPP    --- !---&G&rw@ UwUw0- &Gy& --iyH-- @Times New Roman UwUw0- .2 Multi4. . 2  -. .'2 threaded programming ./. .2 d[ with NSPR++!!'.--Q1-- @Times New Roman UwUw0- .2 WLarry Hardiman  .--՜.+,0T    *& More What Not to Dou1 PRBool dataIsAvailable = PR_FALSE; 2 PR_Lock( lock ); 3 if ( !dataIsAvailable ) 4 PR_WaitCondVar( cv, PR_INTERVAL_NO_TIMEOUT ); /* Bad! dataIsAvailable may change during wait */ 5 /* work on the available data */ 6 PR_Unlock( lock ); The lock is released during PR_WaitCondvar(). Other threads can change dataIsAvailable. Use the while() loop; test, wait, test again."v) #s 0Condvar Example A hacked cvar.c goes here.  High Performance Threads.& you are protecting data, not code partition assets so that you protect the smallest piece possible hold exclusive control as short a time as possible  BibliographyFDavid R. Butenhof, "Programming with POSIX Threads", Addison-Wesley, 1997 Steve Kleiman, et.al,  Programming with Threads , Prentice Hall, 1996 NSPR group,  NSPR Reference , Ch. 1, 3, 5, 6. http://www.mozilla.org/docs/refList/refNSPR/contents.html UseNet newsgroup: comp.programming.threads.L$: ?V &/       #$r$2'( </ 0DTimes New Roman 0 0DLucida Console 0 01  @n?" dd@  @@`` H(n!      !c $@g4FdFd 0x ppp@  <4BdBd g44d4d 0x Vp@ pp<4!d!dL? %O = $Multi-threaded programming with NSPR Larry HardimanOverviewWhat is NSPR? Why program using threads? What you need to program with threads NSPR s thread abstraction Thread synchronization using locks Thread synch using condition variables Techniques for high performance threadsO% What is NSPR?Original NSPR 1.0 was NS Java base NSPR 2.0 began in early 1996 NSPR 2 is platform abstraction No GUI Threading, thread sync File and Socket I/O Time, IPC, ...Why program using threads?}Simplify program design Improve performance when calling blocking functions Utilize all processors on multi-processor systems Thread Programming Basics<Thread Management Thread private data Thread synchronization"+= Thread Management^PR_CreateThread() PR_Interrupt() PR_JoinThread() PR_SetThreadPriority() PR_GetThreadPriority()P   Creating a thread1 #include  prthread.h 2 PRThread * 3 PR_CreateThread( 4 PRThreadType type, /* user or system */ 5 (void)(start)(void *arg), /* function */ 6 void *arg; /*argument to start function */ 7 PRThreadPriority priority, /* low, norm & */ 8 PRThreadScope scope, /* local, global */ 9 PRThreadState state, /* joinable? */ 10 PRInt32 stacksize );kk    5'" !    Thread Private Data\Think of  thread private data as global variables, but for exclusive use for a single thread. Void (*PR_ThreadPrivateDTOR)(void*); PRStatus PR_NewThreadPrivateIndex( int *ndx, (dtor)); PRStatus PR_SetThreadPrivate( ndx, *priv ); void* PR_GetThreadPrivate( ndx ); See: mozilla/nsprpub/pr/tests/tpd.c"/^j     Thread Sync using PRLock21 #include  prlock.h 2 PRLock *lock; 3 lock = PR_NewLock(); 4 PR_Lock(lock); 5 /* operate on your data & */ 6 PR_Unlock(lock); 7 PR_DestroyLock(lock);P L Using Condition Variables1 PRIntn data = 0; /* Thread A */ 2 PRLock *lock = PR_NewLock(); 3 PRCondVar *cvar = PR_NewCondVar(lock); 4 PR_Lock(lock); 5 while( data == 0 ) 6 PR_WaitCondVar( cvar, timeout ); /* lock released while blocked. */ /* re-locked at fall through */ 7 data = 0; /* satisfy Thread B s condition */ 8 PR_NotifyCondVar( cvar ); 9 PR_Unlock( lock );ff    6   Using Condition Variables/* Thread B */ 4 PR_Lock(lock); 5 while( data == 1 ) 6 PR_WaitCondVar( cvar, timeout ); /* lock released while blocked. */ /* re-locked at fall through */ 7 data = 1; 8 PR_NotifyCondVar( cvar ); 9 PR_Unlock( lock ); P= i !What Not to Do1 PRLock *lock; 2 PRCondVar *cv; 3 PR_Lock( lock ); 4 PR_WaitCondVar( cv, PR_INTERVAL_NO_TIMEOUT ); /* implies state in *cv */ 5 /* do something */ 6 PR_Unlock( lock ); There is no state in a PRCondVar..!  6H "*& More What Not to Dou1 PRBool dataIsAvailable = PR_FALSE; 2 PR_Lock( lock ); 3 if ( !dataIsAvailable ) 4 PR_WaitCondVar( cv, PR_INTERVAL_NO_TIMEOUT ); /* Bad! dataIsAvailable may change during wait */ 5 /* work on the available data */ 6 PR_Unlock( lock ); The lock is released during PR_WaitCondvar(). Other threads can change dataIsAvailable. Use the while() loop; test, wait, test again."v) #s 0Condvar Example A hacked cvar.c goes here.  High Performance Threads.& you are protecting data, not code partition assets so that you protect the smallest piece possible hold exclusive control as short a time as possible  BibliographyFDavid R. Butenhof, "Programming with POSIX Threads", Addison-Wesley, 1997 Steve Kleiman, et.al,  Programming with Threads , Prentice Hall, 1996 NSPR group,  NSPR Reference , Ch. 1, 3, 5, 6. http://www.mozilla.org/docs/refList/refNSPR/contents.html UseNet newsgroup: comp.programming.threads.L$: ?V &/       #$  (  l  C %P   l  C %  H  0޽h ? ̙33r#%]=%V'( </ 0DTimes New Roman functions Utilize all processors on multi-processor systems Thread Programming Basics<Thread Management Thread private data Thread synchronization"+= Thread Management^PR_CreateThread() PR_Interrupt() PR_JoinThread() PR_SetThreadPriority() PR_GetThreadPriority()P   0 0DLucida Console 0 01  @n?" dd@  @@`` P)q"      !"c $@g4FdFd 0x ppp@  <4BdBd g44d4d 0x Vp@ pp<4!d!dL? %O = $Multi-threaded programming with NSPR Larry HardimanOverviewWhat is NSPR? Why program using threads? What you need to program with threads NSPR s thread abstraction Thread synchronization using locks Thread synch using condition variables Techniques for high performance threadsO% What is NSPR?Original NSPR 1.0 was NS Java base NSPR 2.0 began in early 1996 NSPR 2 is platform abstraction No GUI Threading, thread sync File and Socket I/O Time, IPC, ...Why program using threads?}Simplify program design Improve performance when calling blocking functions Utilize all processors on multi-processor systems Thread Programming Basics<Thread Management Thread private data Thread synchronization"+= Thread Management^PR_CreateThread() PR_Interrupt() PR_JoinThread() PR_SetThreadPriority() PR_GetThreadPriority()P   Creating a thread1 #include  prthread.h 2 PRThread * 3 PR_CreateThread( 4 PRThreadType type, /* user or system */ 5 (void)(start)(void *arg), /* function */ 6 void *arg; /*argument to start function */ 7 PRThreadPriority priority, /* low, norm & */ 8 PRThreadScope scope, /* local, global */ 9 PRThreadState state, /* joinable? */ 10 PRInt32 stacksize );kk    5'" !    Thread Private Data\Think of  thread private data as global variables, but for exclusive use for a single thread. Void (*PR_ThreadPrivateDTOR)(void*); PRStatus PR_NewThreadPrivateIndex( int *ndx, (dtor)); PRStatus PR_SetThreadPrivate( ndx, *priv ); void* PR_GetThreadPrivate( ndx ); See: mozilla/nsprpub/pr/tests/tpd.c"/^j     Thread Sync using PRLock21 #include  prlock.h 2 PRLock *lock; 3 lock = PR_NewLock(); 4 PR_Lock(lock); 5 /* operate on your data & */ 6 PR_Unlock(lock); 7 PR_DestroyLock(lock);P L Using Condition Variables1 PRIntn data = 0; /* Thread A */ 2 PRLock *lock = PR_NewLock(); 3 PRCondVar *cvar = PR_NewCondVar(lock); 4 PR_Lock(lock); 5 while( data == 0 ) 6 PR_WaitCondVar( cvar, timeout ); /* lock released while blocked. */ /* re-locked at fall through */ 7 data = 0; /* satisfy Thread B s condition */ 8 PR_NotifyCondVar( cvar ); 9 PR_Unlock( lock );ff    6   Using Condition Variables/* Thread B */ 4 PR_Lock(lock); 5 while( data == 1 ) 6 PR_WaitCondVar( cvar, timeout ); /* lock released while blocked. */ /* re-locked at fall through */ 7 data = 1; 8 PR_NotifyCondVar( cvar ); 9 PR_Unlock( lock ); P= i !What Not to Do1 PRLock *lock; 2 PRCondVar *cv; 3 PR_Lock( lock ); 4 PR_WaitCondVar( cv, PR_INTERVAL_NO_TIMEOUT ); /* implies state in *cv */ 5 /* do something */ 6 PR_Unlock( lock ); There is no state in a PRCondVar..!  6H "*& More What Not to Dou1 PRBool dataIsAvailable = PR_FALSE; 2 PR_Lock( lock ); 3 if ( !dataIsAvailable ) 4 PR_WaitCondVar( cv, PR_INTERVAL_NO_TIMEOUT ); /* Bad! dataIsAvailable may change during wait */ 5 /* work on the available data */ 6 PR_Unlock( lock ); The lock is released during PR_WaitCondvar(). Other threads can change dataIsAvailable. Use the while() loop; test, wait, test again."v) #s 0Condvar Example A hacked cvar.c goes here.  High Performance Threads.& you are protecting data, not code partition assets so that you protect the smallest piece possible hold exclusive control as short a time as possible  BibliographyFDavid R. Butenhof, "Programming with POSIX Threads", Addison-Wesley, 1997 Steve Kleiman, et.al,  Programming with Threads , Prentice Hall, 1996 NSPR group,  NSPR Reference , Ch. 1, 3, 5, 6. http://www.mozilla.org/docs/refList/refNSPR/contents.html UseNet newsgroup: comp.programming.threads.L$: ?V &/       #$&  (  l  C %P   l  C %  H  0޽h ? ̙33r0 2* ( p($d R  3    0  C % @   NSPR is no longer Java base. OS vendors supply Java runtime NSPR 2 is a platform abstraction. Used to varying degrees by client and server applications H  0޽h ? ̙33ry% U1&V'( </ 0DTimes New Roman 0 0DLucida Console 0 01  @n?" dd@  @@`` P)q"      !"c $@g4FdFd 0x ppp@  <4BdBd g4IdId 0p@ pp<4!d!dL? %O = $Multi-threaded programming with NSPR Larry HardimanOverviewWhat is NSPR? Why program using threads? What you need to program with threads NSPR s thread abstraction Thread synchronization using locks Thread synch using condition variables Techniques for high performance threadsO% What is NSPR?Original NSPR 1.0 was NS Java base NSPR 2.0 began in early 1996 NSPR 2 is platform abstraction No GUI Threading, thread sync File and Socket I/O Time, IPC, ...Why program using threads?}Simplify program design Improve performance when calling blocking functions Utilize all processors on multi-processor systems Thread Programming Basics<Thread Management Thread private data Thread synchronization"+= Thread Management^PR_CreateThread() PR_Interrupt() PR_JoinThread() PR_SetThreadPriority() PR_GetThreadPriority()P   Creating a thread1 #include  prthread.h 2 PRThread * 3 PR_CreateThread( 4 PRThreadType type, /* user or system */ 5 (void)(start)(void *arg), /* function */ 6 void *arg; /*argument to start function */ 7 PRThreadPriority priority, /* low, norm & */ 8 PRThreadScope scope, /* local, global */ 9 PRThreadState state, /* joinable? */ 10 PRInt32 stacksize );kk    5'" !    Thread Private Data\Think of  thread private data as global variables, but for exclusive use for a single thread. Void (*PR_ThreadPrivateDTOR)(void*); PRStatus PR_NewThreadPrivateIndex( int *ndx, (dtor)); PRStatus PR_SetThreadPrivate( ndx, *priv ); void* PR_GetThreadPrivate( ndx ); See: mozilla/nsprpub/pr/tests/tpd.c"/^j     Thread Sync using PRLock21 #include  prlock.h 2 PRLock *lock; 3 lock = PR_NewLock(); 4 PR_Lock(lock); 5 /* operate on your data & */ 6 PR_Unlock(lock); 7 PR_DestroyLock(lock);P L Using Condition Variables1 PRIntn data = 0; /* Thread A */ 2 PRLock *lock = PR_NewLock(); 3 PRCondVar *cvar = PR_NewCondVar(lock); 4 PR_Lock(lock); 5 while( data == 0 ) 6 PR_WaitCondVar( cvar, timeout ); /* lock released while blocked. */ /* re-locked at fall through */ 7 data = 0; /* satisfy Thread B s condition */ 8 PR_NotifyCondVar( cvar ); 9 PR_Unlock( lock );ff    6   Using Condition Variables/* Thread B */ 4 PR_Lock(lock); 5 while( data == 1 ) 6 PR_WaitCondVar( cvar, timeout ); /* lock released while blocked. */ /* re-locked at fall through */ 7 data = 1; 8 PR_NotifyCondVar( cvar ); 9 PR_Unlock( lock ); P= i !What Not to Do1 PRLock *lock; 2 PRCondVar *cv; 3 PR_Lock( lock ); 4 PR_WaitCondVar( cv, PR_INTERVAL_NO_TIMEOUT ); /* implies state in *cv */ 5 /* do something */ 6 PR_Unlock( lock ); There is no state in a PRCondVar..!  6H "*& More What Not to Dou1 PRBool dataIsAvailable = PR_FALSE; 2 PR_Lock( lock ); 3 if ( !dataIsAvailable ) 4 PR_WaitCondVar( cv, PR_INTERVAL_NO_TIMEOUT ); /* Bad! dataIsAvailable may change during wait */ 5 /* work on the available data */ 6 PR_Unlock( lock ); The lock is released during PR_WaitCondvar(). Other threads can change dataIsAvailable. Use the while() loop; test, wait, test again."v) #s 0Condvar Example A hacked cvar.c goes here.  High Performance Threads.& you are protecting data, not code partition assets so that you protect the smallest piece possible hold exclusive control as short a time as possible  BibliographyFDavid R. Butenhof, "Programming with POSIX Threads", Addison-Wesley, 1997 Steve Kleiman, et.al,  Programming with Threads , Prentice Hall, 1996 NSPR group,  NSPR Reference , Ch. 1, 3, 5, 6. http://www.mozilla.org/docs/refList/refNSPR/contents.html UseNet newsgroup: comp.programming.threads.L$: ?V &/       #$&0 vnP(  PR P 3    t P C D% @   This is the ultra simple case. Thread A, Thread B & do a simple see-saw. No meaningful work. The while is necessary because the lock is released during the wait! Some platforms provide for spurious notifies. You gotta check the variable.H P 0޽h ? ̙33rq<M@&V'( </ 0DTimes New Roman 0 0DLucida Console 0 01  @n?" dd@  @@`` P)q"      !"c $@g4FdFd 0x ppp@  <4BdBd g4IdId 0p@ pp<4!d!dL? %O = $Multi-threaded programming with NSPR Larry HardimanOverviewWhat is NSPR? Why program using threads? What you need to program with threads NSPR s thread abstraction Thread synchronization using locks Thread synch using condition variables Techniques for high performance threadsO% What is NSPR?Original NSPR 1.0 was NS Java base NSPR 2.0 began in early 1996 NSPR 2 is platform abstraction No GUI Threading, thread sync File and Socket I/O Time, IPC, ...Why program using threads?}Simplify program design Improve performance when calling blocking functions Utilize all processors on multi-processor systems Thread Programming Basics<Thread Management Thread private data Thread synchronization"+= Thread Management^PR_CreateThread() PR_Interrupt() PR_JoinThread() PR_SetThreadPriority() PR_GetThreadPriority()P   Creating a thread1 #include  prthread.h 2 PRThread * 3 PR_CreateThread( 4 PRThreadType type, /* user or system */ 5 (void)(start)(void *arg), /* function */ 6 void *arg; /*argument to start function */ 7 PRThreadPriority priority, /* low, norm & */ 8 PRThreadScope scope, /* local, global */ 9 PRThreadState state, /* joinable? */ 10 PRInt32 stacksize );kk    5'" !    Thread Private Data\Think of  thread private data as global variables, but for exclusive use for a single thread. Void (*PR_ThreadPrivateDTOR)(void*); PRStatus PR_NewThreadPrivateIndex( int *ndx, (dtor)); PRStatus PR_SetThreadPrivate( ndx, *priv ); void* PR_GetThreadPrivate( ndx ); See: mozilla/nsprpub/pr/tests/tpd.c"/^j     Thread Sync using PRLock21 #include  prlock.h 2 PRLock *lock; 3 lock = PR_NewLock(); 4 PR_Lock(lock); 5 /* operate on your data & */ 6 PR_Unlock(lock); 7 PR_DestroyLock(lock);P L Using Condition Variables1 PRIntn data = 0; /* Thread A */ 2 PRLock *lock = PR_NewLock(); 3 PRCondVar *cvar = PR_NewCondVar(lock); 4 PR_Lock(lock); 5 while( data == 0 ) 6 PR_WaitCondVar( cvar, timeout ); /* lock released while blocked. */ /* re-locked at fall through */ 7 data = 0; /* satisfy Thread B s condition */ 8 PR_NotifyCondVar( cvar ); 9 PR_Unlock( lock );ff    6   Using Condition Variables/* Thread B */ 4 PR_Lock(lock); 5 while( data == 1 ) 6 PR_WaitCondVar( cvar, timeout ); /* lock released while blocked. */ /* re-locked at fall through */ 7 data = 1; 8 PR_NotifyCondVar( cvar ); 9 PR_Unlock( lock ); P= i !What Not to Do1 PRLock *lock; 2 PRCondVar *cv; 3 PR_Lock( lock ); 4 PR_WaitCondVar( cv, PR_INTERVAL_NO_TIMEOUT ); /* implies state in *cv */ 5 /* do something */ 6 PR_Unlock( lock ); There is no state in a PRCondVar..!  6H "*& More What Not to Dou1 PRBool dataIsAvailable = PR_FALSE; 2 PR_Lock( lock ); 3 if ( !dataIsAvailable ) 4 PR_WaitCondVar( cv, PR_INTERVAL_NO_TIMEOUT ); /* Bad! dataIsAvailable may change during wait */ 5 /* work on the available data */ 6 PR_Unlock( lock ); The lock is released during PR_WaitCondvar(). Other threads can change dataIsAvailable. Use the while() loop; test, wait, test again."v) #s 0Condvar Example A hacked cvar.c goes here.  High Performance Threads.& you are protecting data, not code partition assets so that you protect the smallest piece possible hold exclusive control as short a time as possible  BibliographyFDavid R. Butenhof, "Programming with POSIX Threads", Addison-Wesley, 1997 Steve Kleiman, et.al,  Programming with Threads , Prentice Hall, 1996 NSPR group,  NSPR Reference , Ch. 1, 3, 5, 6. http://www.mozilla.org/docs/refList/refNSPR/contents.html UseNet newsgroup: comp.programming.threads.L$: ?V &/       #$&0 Q(   R  3      C % @   _KRecall that some platforms permit spurious wake up of a condition variable H  0޽h ? ̙33r@$'h@0j&>&( </ 0DTimes New Roman 0 0DLucida Console 0 01  @n?" dd@  @@`` P)q            c $@g4FdFd 0x ppp@  <4BdBd g4IdId 0p@ pp<4!d!dL? %O = $Multi-threaded programming with NSPR Larry HardimanOverviewWhat is NSPR? Why program using threads? What you need to program with threads NSPR s thread abstraction Thread synchronization using locks Thread synch using condition variables Techniques for high performance threadsO% What is NSPR?Original NSPR 1.0 was NS Java base NSPR 2.0 began in early 1996 NSPR 2 is platform abstraction No GUI Threading, thread sync File and Socket I/O Time, IPC, ...Why program using threads?}Simplify program design Improve performance when calling blocking functions Utilize all processors on multi-processor systems Thread Programming Basics<Thread Management Thread private data Thread synchronization"+= Thread Management^PR_CreateThread() PR_Interrupt() PR_JoinThread() PR_SetThreadPriority() PR_GetThreadPriority()P   Creating a thread1 #include  prthread.h 2 PRThread * 3 PR_CreateThread( 4 PRThreadType type, /* user or system */ 5 (void)(start)(void *arg), /* function */ 6 void *arg; /*argument to start function */ 7 PRThreadPriority priority, /* low, norm & */ 8 PRThreadScope scope, /* local, global */ 9 PRThreadState state, /* joinable? */ 10 PRInt32 stacksize );kk    5'" !    Thread Private Data\Think of  thread private data as global variables, but for exclusive use for a single thread. Void (*PR_ThreadPrivateDTOR)(void*); PRStatus PR_NewThreadPrivateIndex( int *ndx, (dtor)); PRStatus PR_SetThreadPrivate( ndx, *priv ); void* PR_GetThreadPrivate( ndx ); See: mozilla/nsprpub/pr/tests/tpd.c"/^j     Thread Sync using PRLock21 #include  prlock.h 2 PRLock *lock; 3 lock = PR_NewLock(); 4 PR_Lock(lock); 5 /* operate on your data & */ 6 PR_Unlock(lock); 7 PR_DestroyLock(lock);P L Using Condition Variables1 PRIntn data = 0; /* Thread A */ 2 PRLock *lock = PR_NewLock(); 3 PRCondVar *cvar = PR_NewCondVar(lock); 4 PR_Lock(lock); 5 while( data == 0 ) 6 PR_WaitCondVar( cvar, timeout ); /* lock released while blocked. */ /* re-locked at fall through */ 7 data = 0; /* satisfy Thread B s condition */ 8 PR_NotifyCondVar( cvar ); 9 PR_Unlock( lock );ff    6   Using Condition Variables/* Thread B */ 4 PR_Lock(lock); 5 while( data == 1 ) 6 PR_WaitCondVar( cvar, timeout ); /* lock released while blocked. */ /* re-locked at fall through */ 7 data = 1; 8 PR_NotifyCondVar( cvar ); 9 PR_Unlock( lock ); P= i !What Not to Do1 PRLock *lock; 2 PRCondVar *cv; 3 PR_Lock( lock ); 4 PR_WaitCondVar( cv, PR_INTERVAL_NO_TIMEOUT ); /* implies state in *cv */ 5 /* do something */ 6 PR_Unlock( lock ); There is no state in a PRCondVar..!  6H "*& More What Not to Dou1 PRBool dataIsAvailable = PR_FALSE; 2 PR_Lock( lock ); 3 if ( !dataIsAvailable ) 4 PR_WaitCondVar( cv, PR_INTERVAL_NO_TIMEOUT ); /* Bad! dataIsAvailable may change during wait */ 5 /* work on the available data */ 6 PR_Unlock( lock ); The lock is released during PR_WaitCondvar(). Other threads can change dataIsAvailable. Use the while() loop; test, wait, test again."v) #s 0Condvar Example A hacked cvar.c goes here.  High Performance Threads.& you are protecting data, not code partition assets so that you protect the smallest piece possible hold exclusive control as short a time as possible  BibliographyFDavid R. Butenhof, "Programming with POSIX Threads", Addison-Wesley, 1997 Steve Kleiman, et.al,  Programming with Threads , Prentice Hall, 1996 NSPR group,  NSPR Reference , Ch. 1, 3, 5, 6. http://www.mozilla.org/docs/refList/refNSPR/contents.html UseNet newsgroup: comp.programming.threads.L$: ?V &/   &H 0 x( ̙33 xR x 3    & x C ~ @  & ^No thread_exit(). Bad programming practice. You must  un-roll the stack to cleanup locks, resources, memory PR_Interrupt() co-operative kill. Works at NSPR blocking calls. ,6vH x 0޽h ? ̙33rlj Hj&p'( </ 0DTimes New Roman 0 0DLucida Console 0 01  @n?" dd@  @@`` `+p"      ! #$c $@g4adad 0ppp@  <4BdBdg4IdId 0p@ pp<4!d!dL? %O = $Multi-threaded programming with NSPR Larry HardimanOverviewWhat is NSPR? Why program using threads? What you need to program with threads NSPR s thread abstraction Thread synchronization using locks Thread synch using condition variables Techniques for high performance threadsO% What is NSPR?Original NSPR 1.0 was NS Java base NSPR 2.0 began in early 1996 NSPR 2 is platform abstraction No GUI Threading, thread sync File and Socket I/O Time, IPC, ...Why program using threads?}Simplify program design Improve performance when calling blocking functions Utilize all processors on multi-processor systems Thread Programming Basics<Thread Management Thread private data Thread synchronization"+= Thread Management^PR_CreateThread() PR_Interrupt() PR_JoinThread() PR_SetThreadPriority() PR_GetThreadPriority()P   Creating a thread1 #include  prthread.h 2 PRThread * 3 PR_CreateThread( 4 PRThreadType type, /* user or system */ 5 (void)(start)(void *arg), /* function */ 6 void *arg; /*argument to start function */ 7 PRThreadPriority priority, /* low, norm & */ 8 PRThreadScope scope, /* local, global */ 9 PRThreadState state, /* joinable? */ 10 PRInt32 stacksize );kk    5'" !    Thread Private Data\Think of  thread private data as global variables, but for exclusive use for a single thread. Void (*PR_ThreadPrivateDTOR)(void*); PRStatus PR_NewThreadPrivateIndex( int *ndx, (dtor)); PRStatus PR_SetThreadPrivate( ndx, *priv ); void* PR_GetThreadPrivate( ndx ); See: mozilla/nsprpub/pr/tests/tpd.c"/^j    'Thread sync using PRLock41 #include  prlock.h 2 PRLock *lock; 3 lock = PR_NewLock(); 4 PR_Lock(lock); 5 /* operate on your data & */ 6 PR_Unlock(lock); 7 PR_DestroyLock(lock);  P L  Using Condition Variables1 PRIntn data = 0; /* Thread A */ 2 PRLock *lock = PR_NewLock(); 3 PRCondVar *cvar = PR_NewCondVar(lock); 4 PR_Lock(lock); 5 while( data == 0 ) 6 PR_WaitCondVar( cvar, timeout ); /* lock released while blocked. */ /* re-locked at fall through */ 7 data = 0; /* satisfy Thread B s condition */ 8 PR_NotifyCondVar( cvar ); 9 PR_Unlock( lock );ff    6   Using Condition Variables/* Thread B */ 4 PR_Lock(lock); 5 while( data == 1 ) 6 PR_WaitCondVar( cvar, timeout ); /* lock released while blocked. */ /* re-locked at fall through */ 7 data = 1; 8 PR_NotifyCondVar( cvar ); 9 PR_Unlock( lock ); P= i !What Not to Do1 PRLock *lock; 2 PRCondVar *cv; 3 PR_Lock( lock ); 4 PR_WaitCondVar( cv, PR_INTERVAL_NO_TIMEOUT ); /* implies state in *cv */ 5 /* do something */ 6 PR_Unlock( lock ); There is no state in a PRCondVar..!  6H "*& More What Not to Dou1 PRBool dataIsAvailable = PR_FALSE; 2 PR_Lock( lock ); 3 if ( !dataIsAvailable ) 4 PR_WaitCondVar( cv, PR_INTERVAL_NO_TIMEOUT ); /* Bad! dataIsAvailable may change during wait */ 5 /* work on the available data */ 6 PR_Unlock( lock ); The lock is released during PR_WaitCondvar(). Other threads can change dataIsAvailable. Use the while() loop; test, wait, test again."v) #s 0Condvar Example A hacked cvar.c goes here.  High Performance Threads.& you are protecting data, not code partition assets so that you protect the smallest piece possible hold exclusive control as short a time as possible  BibliographyFDavid R. Butenhof, "Programming with POSIX Threads", Addison-Wesley, 1997 Steve Kleiman, et.al,  Programming with Threads , Prentice Hall, 1996 NSPR group,  NSPR Reference , Ch. 1, 3, 5, 6. http://www.mozilla.org/docs/refList/refNSPR/contents.html UseNet newsgroup: comp.programming.threads.L$: ?V &/      #$&(  0(  l  C ~P  ~ l  C ~ ~ H  0޽h ? ̙33Z0 ,( ]mO ,R , 3    & , C ~ @  & pType := system, user. & An artifact of when NSPR was the base for NS Java VM. & when all user threads end, the process terminates. & Create your threads as user threads. Scope := local, global, global_bound local = user thread, scheduled by NSPR global = user or kernel thread. Varies depending on platform. You do this. Global_bound = a kernel thread. State := joinable, not-joinable Priority = limited range: low, normal, high, urgent. ,k  6H , 0޽h ? ̙33(0 Px(  PR P 3    ~ P C t~ @  ~ ,This is the ultra simple case. Thread A, Thread B & do a simple see-saw. No meaningful work. The while is necessary because the lock is released during the wait! Some platforms provide for spurious notifies. You gotta check the variable. PR_NotifyAll() notifies all waiters. Timeout is indistinguishable from normal notify, spurious notify. Remember! The state is in the user defined state variables. > H P 0޽h ? ̙33 0 L( 0@ R  3    ~  C T~ @  ~ Z$Protecting access to data, not a path through code Instead of locking a large aggregate, lock a piece avoid holding lock across a blocking call avoid holding lock across change in lexical scope (any function call) avoid holding lock across malloc() free() or other CPU intensive activity. -H  0޽h ? ̙33f0 &@(  R  3    ~$  C ~ @  ~ `PRLock is not re-entrant. Attempting to lock a PRLock you already hold will fail No TryLock() H(H  0޽h ? ̙33r,> (' , (((Root EntrydO){r/@ Current UserSSummaryInformation(pPowerPoint Document(<Ub   !"#$%&'()*+,-./0123456789:MK>?@ABCDEFGHIJeLSNOPQRVTWXYZ[\]^_`a=cd;fghijklmnopqrstuvwxyz{|}~, / !"#$%&'()*+0)_Lawrence HardimanLawrenceOh+'0@ hp    Using Threads with NSPRlarryhhLawrence Hardiman N39rMicrosoft PowerPointPR@*<@.P@q@r/4Gg  '& &&# to Do More What Not to DoCondvar ExampleHigh Performance Threads Bibliography  Fonts UsedDesign Template Slide Titleside Titles 6> HardimanAN{CBA95320-6D83-11D4-B97D-00805F369FB3}  Hardiman--iyH"System 0-& Creating a thread1 #include  prthread.h 2 PRThread * 3 PR_CreateThread( 4 PRThreadType type, /* user or system */ 5 (void)(start)(void *arg), /* function */ 6 void *arg; /*argument to start function */ 7 PRThreadPriority priority, /* low, norm & */ 8 PRThreadScope scope, /* local, global */ 9 PRThreadState state, /* joinable? */ 10 PRInt32 stacksize );kk    5'" ! $   Thread Private Data\Think of  thread private data as global variables, but for exclusive use for a single thread. Void (*PR_ThreadPrivateDTOR)(void*); PRStatus PR_NewThreadPrivateIndex( int *ndx, (dtor)); PRStatus PR_SetThreadPrivate( ndx, *priv ); void* PR_GetThreadPrivate( ndx ); See: mozilla/nsprpub/pr/tests/tpd.c"/^j    'Thread sync using PRLock41 #include  prlock.h 2 PRLock *lock; 3 lock = PR_NewLock(); 4 PR_Lock(lock); 5 /* operate on your data & */ 6 PR_Unlock(lock); 7 PR_DestroyLock(lock);  P L  Using Condition Variables1 PRIntn data = 0; /* Thread A */ 2 PRLock *lock = PR_NewLock(); 3 PRCondVar *cvar = PR_NewCondVar(lock); 4 PR_Lock(lock); 5 while( data == 0 ) 6 PR_WaitCondVar( cvar, timeout ); /* lock released while blocked. */ /* re-locked at fall through */ 7 data = 0; /* satisfy Thread B s condition */ 8 PR_NotifyCondVar( cvar ); 9 PR_Unlock( lock );ff    6   Using Condition Variables/* Thread B */ 4 PR_Lock(lock); 5 while( data == 1 ) 6 PR_WaitCondVar( cvar, timeout ); /* lock released while blocked. */ /* re-locked at fall through */ 7 data = 1; 8 PR_NotifyCondVar( cvar ); 9 PR_Unlock( lock ); P= i !What Not to Do1 PRLock *lock; 2 PRCondVar *cv; 3 PR_Lock( lock ); 4 PR_WaitCondVar( cv, PR_INTERVAL_NO_TIMEOUT ); /* implies state in *cv */ 5 /* do something */ 6 PR_Unlock( lock ); There is no state in a PRCondVar..!  6H "*& More What Not to Dou1 PRBool dataIsAvailable = PR_FALSE; 2 PR_Lock( lock ); 3 if ( !dataIsAvailable ) 4 PR_WaitCondVar( cv, PR_INTERVAL_NO_TIMEOUT ); /* Bad! dataIsAvailable may change during wait */ 5 /* work on the available data */ 6 PR_Unlock( lock ); The lock is released during PR_WaitCondvar(). Other threads can change dataIsAvailable. Use the while() loop; test, wait, test again."v) #s 0Condvar Example A hacked cvar.c goes here.  High Performance Threads.& you are protecting data, not code partition assets so that you protect the smallest piece possible hold exclusive control as short a time as possible  BibliographyPDavid R. Butenhof, "Programming with POSIX Threads", Addison-Wesley, 1997 Steve Kleiman, et.al,  Programming with Threads , Prentice Hall, 1996 NSPR group,  NSPR Reference , Ch. 1, 3, 5, 6. http://www.mozilla.org/projects/nspr/reference/html/index.html UseNet newsgroup: comp.programming.threads.N)Z?P ?rG/      #$&(   \(  \l \ C $2P  2 l \ C 2 2 H \ 0޽h ? ̙33{0 ;3( p($d R  3    x9  C $x @  x NSPR is no longer Java base. OS vendors supply Java runtime NSPR 2 is a platform abstraction. Used to varying degrees by client and server applications NSPR is a design-base, not a porting layer.H  0޽h ? ̙33r&  a(sTNPP &