Multi-threaded programming with NSPR - PowerPoint PPT Presentation

About This Presentation
Title:

Multi-threaded programming with NSPR

Description:

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 ... – PowerPoint PPT presentation

Number of Views:109
Avg rating:3.0/5.0
Slides: 17
Provided by: Lar129
Category:

less

Transcript and Presenter's Notes

Title: Multi-threaded programming with NSPR


1
Multi-threaded programming with NSPR
  • Larry Hardiman

2
Overview
  • What is NSPR?
  • Why program using threads?
  • What you need to program with threads
  • NSPRs thread abstraction
  • Thread synchronization using locks
  • Thread synch using condition variables
  • Techniques for high performance threads

3
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, ...

4
Why program using threads?
  • Simplify program design
  • Improve performance when calling blocking
    functions
  • Utilize all processors on multi-processor systems

5
Thread Programming Basics
  • Thread Management
  • Thread private data
  • Thread synchronization

6
Thread Management
  • PR_CreateThread()
  • PR_Interrupt()
  • PR_JoinThread()
  • PR_SetThreadPriority()
  • PR_GetThreadPriority()

7
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 )

8
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

9
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)

10
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 Bs condition /
  • 8 PR_NotifyCondVar( cvar )
  • 9 PR_Unlock( lock )

11
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 )

12
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.

13
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() looptest, wait, test again.

14
Condvar Example
  • A hacked cvar.c goes here.

15
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

16
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/htm
    l/index.html
  • UseNet newsgroup comp.programming.threads.
Write a Comment
User Comments (0)
About PowerShow.com