This lecture introduces threading models and a few simple examples.
Readings and Activities for the Week:
- Complete and submit Project #3.
- Read class text Chapters #14 and #22.
- Finish work on Project #3 and submit.
Glossary of Terms
-
Thread:
An operating system scheduled execution of a specified function that may run concurrently on its own core, or be premptively switched to simulate concurrency.- A thread shares the process address space with other threads, if any.
- Each active thread has its own stack, that has as its root the stack frame for the function it executes.
- When a thread exits its starting function it is terminated by Windows.
- Any resources shared between threads need to be protected with locks if they are not inherently thread-safe.
- .Net threads are created and managed with a .Net Thread class.
- There is not necessarily a one-to-one correspondence between .Net threads and Windows threads.
-
Lock:
A .Net construct used to insure that access to shared resources is provided to only one thread at a time. -
Race Condition:
Program behavior that depends on timing of operations of multiple threads, often caused by inadequate locking.
- The Syllabus describes topics to be covered, reading material for you to digest as part of each lecture, and due dates for assigned projects.
-
Projects:
-
Midterm Exam
You may wish to look ahead at Topics and materials to study for the Midterm Examination in next week's lecture page Wk #8a. -
Basic Windows Concepts
-
Windows Processes
-
Threads
-
Code Demos:
Starting Threads, Code Snaps
Discusses several ways to create .Net threads. Basic Thread Demos, WPF Thread Demos
Fairly simple examples of thread applications. Blocking Queue using Monitor, Code Snap
Blocking Queues are very useful for sending messages between threads - helps to keep threading models simple. Queued Messages with BlockingQueue
This is a queueing demo. Abstract Task Model - BasicTasks project
.Net Tasks simplify communication between parent and child threads. async await pattern - WpfWithAsync project
Using tasks in GUI applications. WPF Dispatcher Invoke Demo
Child threads are not allowed to directly communicate with a parent window.
Here's how to manage child thread to UI-thread communication. .Net FormInvoke Demo
Same demo, but for legacy WinForms. -
Preparation for Synchronous Discussion:
-
Write pseudo code for a small threading demonstration program that:
- Starts two child threads.
- In the first child thread, create the text for a string.
- The second child then displays the string contents on the console.
You will need to lock the string when reading or writing its contents.
Be prepared to discuss your solution.
-
Write pseudo code for a small threading demonstration program that:
-
Lecture Take-aways:
The most important things we discussed are:- Basic Windows Concepts:
- Threads: