Swimming, not drowning in the 'NET Thread Pool - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Swimming, not drowning in the 'NET Thread Pool

Description:

... with Visual Studio .NET, Richard Grimes, DevelopMentor series. Essential .NET, Don Box, DevelopMentor series. Download Me From. www.DotNetPerformance.com ... – PowerPoint PPT presentation

Number of Views:81
Avg rating:3.0/5.0
Slides: 22
Provided by: nicholas55
Category:

less

Transcript and Presenter's Notes

Title: Swimming, not drowning in the 'NET Thread Pool


1
Swimming, not drowning in the .NET Thread Pool
  • .NET Threads and the Thread Pool
  • Nick Wienholt
  • Nick_at_DotNetPerformance.com

2
Nick Wienholt
  • Windows and .NET systems software engineer/
    architect
  • Microsoft Certified Solution Developer
  • Lead software engineer (Microsoft Technologies)
    Softbuild
  • Author of upcoming book of .NET performance (Q1
    2003)

3
Threading before .NET a reflection on the past
4
Agenda
  • Threading why, how and when
  • Thread Pool
  • Monitoring Threads
  • The Synch Generator a VS.NET add-in

5
Basic Terminology
  • Thread path of code execution. Has a call
    stack where functions and data are placed, and is
    the base unit for which processor time is
    scheduled.
  • Thread safe a piece of code is thread safe
    when it has inbuilt protection against data
    corruption that can be caused by simultaneous
    modification from multiple threads.

6
Threading Before .NET
  • Threading was a bit of a mess.
  • Different languages had different levels of
    support for threading.
  • Some language runtimes where not thread-safe.
  • Different libraries had different levels of
    thread safety.
  • C had many competing libraries with overlapping
    functionality (Win32, C runtime, MFC, ATL\ COM).
  • Operating system dependant Win9x and WinNT
    based systems had different feature sets.

7
Threading in .NET
  • Writing multi-threaded code possible in all
    languages.
  • One unified API for dealing with threads.
  • Improved documentation that indicates the
    thread-safety of the Base Class Library (BCL)
    types.
  • Everything designed for the ground-up with
    awareness of threading issues.

8
New Threading Features
  • Thread pool.
  • Clear and consistent methods for terminating
    threads (Thread.Abort).
  • Language level support for data locking (C lock
    statement, VB.NET SyncLock statement).
  • Threading patterns in various sections of the BCL
    to support light-weight synchronization.
  • ReaderWriter lock provides in-built support for
    allowing multiple readers or a single writer on a
    data structure.

9
Multiple threads when to use them
  • Support asynchronous operations.
  • Read a large file from disk and keep the UI
    responsive.
  • Service multiple client requests simultaneously.
  • Conduct operations at a higher priority
  • Thread priority is specified when a thread is
    created. A thread with higher priority gets a
    large slice of processor time.

10
Thread Synchronization
  • System.Threading.Monitor class can be used to
    prevent multiple threads from modifying data
    structures simultaneously.
  • The lock\ SyncLock statements in C\ VB.NET use a
    Monitor lock in a try block and release the
    Monitor in finally block.

11
Writing a thread-safe class
  • public class ThreadSafe
  • private long _data
  • public long Data
  • get
  • lock(this)return _data
  • set
  • lock(this)_data value

12
Thread safety
  • The vast majority .NET BCL is NOT thread safe.
  • Paths to thread safety
  • Immutable types (e.g. System.String)
  • Synchronization wrapper (e.g. System.Collections.A
    rrayList) (demo)
  • Synchronized contexts very similar in concept
    to COM apartments. Heavy-weight and slow.
  • Hand-coded wrappers.
  • Dont share objects between threads.

13
Using Threads
  • Code demo manually creating a new thread.

14
Thread Pool
  • Motivation threads are expensive to create and
    cheap to recycle.
  • Too many threads in a process hurts performance
    context thrashing
  • The need for a common thread management tool that
    all components and classes used the end of
    hand-code hacks

15
Thread Pool the internals
  • Defaults to 25 threads per process per processor.
  • Can be configured by the runtime host and the
    managed code. (DEMO)
  • Machine.config and web.config can specify default.

16
Thread Pool when to use
  • Servicing requests for multiple clients the web
    server model
  • Waiting on OS handles to become signalled Win32
    events
  • Code demos

17
Thead Pool when not to use
  • Need to set thread priority
  • Long running requests.
  • Thread security token and call-context issues gt
    the big thread pool gotcha.
  • System.Threading.Timer does NOT propagate call
    content or security permissions
  • New threads and queued worker items inherit
    security but NOT call context.

18
Tools for monitoring threads
  • Task Manager
  • Perfmon
  • Process Explorer (www.sysinternals.com)
  • VS.NET be sure to enable unmanaged debugging.
  • DeadlockDetection utility from Debugging
    Applications (MSPRESS) by John Robbins

19
Thread Synchronization Made Easy
  • Demo
  • When not to use
  • Transactional semantics.
  • ReaderWriter lock optimizations.
  • Manual lock granularity optimizations.

20
References
  • MSDN documentation Jan 2002 MSDN is much more
    complete than the VS.NET RTM docs
  • Mike Woodrings web site www.BearCanyon.com
  • Applied .NET Framework Programming, Jeffrey
    Richter, Microsoft Press
  • Developing Applications with Visual Studio .NET,
    Richard Grimes, DevelopMentor series
  • Essential .NET, Don Box, DevelopMentor series

21
Download Me From
www.DotNetPerformance.com All code and slides
Write a Comment
User Comments (0)
About PowerShow.com