Multithread Programming - PowerPoint PPT Presentation

About This Presentation
Title:

Multithread Programming

Description:

execution trace interrupt handler ... three threads in two processes are interleaved on the processor. - Execution passes from one thread to another either when the ... – PowerPoint PPT presentation

Number of Views:76
Avg rating:3.0/5.0
Slides: 9
Provided by: uae48
Learn more at: http://cs351.cs.ua.edu
Category:

less

Transcript and Presenter's Notes

Title: Multithread Programming


1
Multithread Programming
  • Why Multiple Threads?
  • Processes vs. Threads
  • Threads vs. Functions (Processes vs. Programs)
  • Thread States (Unstarted, Running, WaitSleepJoin,
    SuspendRequested, Suspended, AbortRequested,
    Aborted, StopRequested, Stopped)
  • Thread Priority (Highest, AboveNormal, Normal,
    BelowNormal, Lowest)
  • Thread Scheduling
  • Thread Synchronization

2
execution trace
concurrent I/O operation
interrupt handler (routine)
3
  • Take the previous GUI lectures and Proj 3 as an
    example,
  • Form1 and myDialog.
  • Multitasking the program? (Can we work on two
    additions simultaneously?)
  • (of course, if we invoke MyDialog in a
    non-blocked way)
  • Multithreaded programming
  • Split a program into multiple segments (with
    beginning and ending points)
  • Run some of them concurrently
  • On a single processor (CPU) machine, time-sharing
    by multiple threads
  • True for multi-core, Hyper-Threading

4
Multithreading on a Uniprocessorinterleaving of
multiple threads within multiple processes
5
Execution states of a thread
6
Using Threads in C/CLI
  • Method One
  • Create a task
  • Create a thread to associate with the task
  • Start the thread
  • Wait for a thread to end
  • Runs in foreground or background
  • Create an instance of thread. Use a delegate
    ParameterizedThreadStart to place the method to
    be the thread
  • Start() the thread
  • A thread can Sleep()
  • Thread Sleep(2000) // 2000
    milliseconds
  • One thread waits for another thread to finish
    before continuing, Join()

7
Using Threads in C/CLI
  • Method Two thread pool
  • Create a task
  • Queue the task to the thread pool
  • Thread pool is system-managed, easy to use.
  • Run in background, so the main needs to remain
    alive
  • Add to the pool ThreadPoolQueueUserWorkItem()
    //true if added
  • Takes WaitCallback() delegate (so to run your
    thread task)
  • Takes an Object handle (so to pass info to the
    thread task)

8
Examples
Write a Comment
User Comments (0)
About PowerShow.com