Title: Multithreading
1Multithreading
- 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.
2Multithreading
- 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
3Thread 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.
4Thread Syntax
- Declare functions that will become threads
- VOID ThreadFunc(Void pvoid)
- function code
- Create thread
- _beginthread (ThreadFunc, 0, NULL)
5Example
- 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)
-
-
6LRESULT CALLBACK WndProc (hwnd, msg, wParam,
lParam)
- switch (msg)
- case WM_CREATE
- _beginthread (ThreadFunc, 0, NULL)
-
-
ThreadFunc
Main Thread
CPU
7LRESULT 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() ..
9VOID 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
10LRESULT 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
11LRESULT CALLBACK WndProc (hwnd, msg, wParam,
lParam)
- switch (msg)
- case WM_CREATE
- _beginthread (ThreadFunc, 0, NULL)
- _beginthread (ThreadFunc, 0, NULL)
-
-
ThreadFunc
Main Thread
ThreadFunc
CPU
12Shared 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
13Thread 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.
14Race 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.
15Assume global cxClient, cyClient LRESULT
CALLBACK WndProc(..) .. case
WM_SIZE cxClient LOWORD(lParam) cyClient
HIWORD(lParam) ..
16In Thread Void thread1(PVOID pvoid) .
TextOut(buf, cxClient, cyClient/4 ..)
17Possible 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.
18Solutions
- 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.