Multithreading - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Multithreading

Description:

OS support for creating threads, terminating threads, and preemptively switches ... Void thread1(PVOID pvoid) TextOut(buf, cxClient, cyClient/4 .) Possible Schedule ... – PowerPoint PPT presentation

Number of Views:78
Avg rating:3.0/5.0
Slides: 19
Provided by: phi140
Category:

less

Transcript and Presenter's Notes

Title: Multithreading


1
Multithreading
  • Allows application to split itself into multiple
    threads of execution (threads of execution).
  • OS support for creating threads, terminating
    threads, and preemptively switches control among
    threads.
  • A thread is simply a function.
  • Can call other functions
  • The same function can be used to create multiple
    threads.
  • Once created, all threads execute concurrently.

2
Multithreading
  • Threads are part of same process and share all
    process resources
  • Memory
  • Open Files
  • Global Variables
  • Static Variables (of same function)
  • Each thread has its own
  • processor (and math coprocessor) state
  • stack

3
Thread Context
  • Thread context (e.g., processor state and stack)
    saved and restored during thread switches.
  • Multithreading can be extremely difficult (if not
    done correctly).
  • All threads shared global variables and static
    variables of thread function.
  • Can be preempted at any time.

4
Thread Syntax
  • Declare functions that will become threads
  • VOID ThreadFunc(Void pvoid)
  • function code
  • Create thread
  • _beginthread (ThreadFunc, 0, NULL)

5
Example
  • LRESULT WinAPI WinMain(.)
  • // Declare window class, create window(s)
    etc.
  • VOID ThreadProc(PVOID pvoid)
  • .
  • LRESULT CALLBACK WndProc (hwnd, msg, wParam,
    lParam)
  • switch (msg)
  • case WM_CREATE
  • _beginthread (ThreadFunc, 0, NULL)

6
LRESULT CALLBACK WndProc (hwnd, msg, wParam,
lParam)
  • switch (msg)
  • case WM_CREATE
  • _beginthread (ThreadFunc, 0, NULL)

ThreadFunc
Main Thread
CPU
7
LRESULT CALLBACK WndProc (hwnd, msg, wParam,
lParam)
  • switch (msg)
  • case WM_CREATE
  • _beginthread (ThreadFunc, 0, NULL)
  • _beginthread (ThreadFunc, 0, NULL)

ThreadFunc
Main Thread
ThreadFunc
CPU
8
// GLOBAL VARIABLES. Accessible to all
threads. include ltwindow.hgt include
ltprocess.hgt int cxClient, cyClient HWND
my_hwnd
int WINAPI WinMain() ..
9
VOID ThreadFunc(Void pvoid) static int
Doober 0 /Shared by all threads created
with this func/ HDC hdc /
Automatic variable, private to all threads /
while(1) hdc GetDC(my_hwnd)
DrawText(hdc, Message, ..)
ReleaseDC(hdc) Doober 2

10
LRESULT CALLBACK WndProc (hwnd, msg, wParam,
lParam)
  • switch (msg)
  • case WM_CREATE
  • my_hwnd hwnd // All threads now have
    handle to parent window.
  • _beginthread (ThreadFunc, 0, NULL)

ThreadFunc
Main Thread
Share cxClient, cyClient, my_hwnd
CPU
11
LRESULT CALLBACK WndProc (hwnd, msg, wParam,
lParam)
  • switch (msg)
  • case WM_CREATE
  • _beginthread (ThreadFunc, 0, NULL)
  • _beginthread (ThreadFunc, 0, NULL)

ThreadFunc
Main Thread
ThreadFunc
CPU
12
Shared Among all Threads
my_hwnd, cxClient, cyClient
ThreadFunc
ThreadFunc
Main Thread
Shared between thread functions.
Doober
Private copy for each ThreadFunc function.
HDC hdc
HDC hdc
13
Thread Context
  • Thread context (e.g., processor state and stack)
    saved and restored during thread switches.
  • Multithreading can be extremely difficult (if not
    done correctly).
  • All threads shared global variables and static
    variables.
  • Can be preempted at any time.

14
Race Conditions
  • Errors that occur on a very infrequent basis.
  • Happen at random times so very difficult to
    isolate and fix.
  • Race conditions caused by programmer assuming
    that one thread will complete operation on data
    before another thread will access data.
  • That is, programmer makes assumptions as to the
    order in which threads are executed.

15
Assume global cxClient, cyClient LRESULT
CALLBACK WndProc(..) .. case
WM_SIZE cxClient LOWORD(lParam) cyClient
HIWORD(lParam) ..
16
In Thread Void thread1(PVOID pvoid) .
TextOut(buf, cxClient, cyClient/4 ..)
17
Possible Schedule
  • Main thread cxClient LOWORD(lParam)
  • Interrupted
  • Other thread TextOut(buf, cxClient, cyClient/4,
    )
  • Interrupted
  • Main thread cyClient HIWORD(lParam)
  • Thread TextOut statement used new cx_Client and
    old value of cyClient. Inconsistent.

18
Solutions
  • Synchronization mechanisms been active area of
    research for last 30 years.
  • Structure your use of threads so that race
    conditions cannot develop.
  • Structure so that primary thread creates all
    windows, all window procedures, and processes all
    messages to window.
  • Thread then becomes background cruncher that
    does not handle user input.
Write a Comment
User Comments (0)
About PowerShow.com