Allyson Eno Unit designed using The Brain Based Teaching Model (Hardiman, 2003)
Multi-threaded programming with NSPR Larry Hardiman.
-
Upload
geoffrey-thompson -
Category
Documents
-
view
216 -
download
1
Transcript of Multi-threaded programming with NSPR Larry Hardiman.
Multi-threaded programming with NSPR
Larry Hardiman
Overview
• What 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 threads
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()
Creating a thread
1 #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 );
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
Thread sync using PRLock
1 #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);
Using Condition Variables
1 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 );
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 );
What Not to Do
1 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.
… More What Not to Do
1 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.
Condvar 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
Bibliography
• David 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.