Presentation is loading. Please wait.

Presentation is loading. Please wait.

Multithread Programming

Similar presentations


Presentation on theme: "Multithread Programming"— Presentation transcript:

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 Uniprocessor interleaving of multiple threads within multiple processes
On a uniprocessor, multiprogramming enables the interleaving of multiple threads within multiple processes. In the example, - three threads in two processes are interleaved on the processor. - Execution passes from one thread to another either when the currently running thread is blocked or its time slice is exhausted.

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: ThreadPool::QueueUserWorkItem() //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


Download ppt "Multithread Programming"

Similar presentations


Ads by Google