
Handouts/CoreTechnologies/ThreadsAndSynchronization/Code/
Threads
Threads
Purpose:
Encapsulate Win32 thread creation and use in an easy to use C++ class. The
Threads module provides two types of threads:
1. Thread<default_thread>, equivalent to thread:
Threads of this type may be created either on the stack or heap.
a. If you create them on the heap you should eventually call delete on the pointer
that new thread returns.
b. Thread<default_thread> objects use a pointer to the Thread_Processing object
created by the application, so the application must not overwrite that object.
2. Thread<terminating_thread>, equivalent to tthread:
These thread objects assume they’ve been created on the heap:
a. They call “delete this” when their thread processing has finished.
b. These objects make a clone of the object whose class is derived from
Thread_Processing object, and delete that when their thread function completes.
That means that it is unimportant whether the application overwrites the original.
c. You should not use Thread<terminating_thread> non-static member functions
after calling start, as the object’s destruction is non-deterministic. There is only
one static member function, wait(HANDLE tHandle). That allows you to wait for
an object that may already be destroyed, in which case you return from the wait
immediately.
This supports a “fire and forget” model.
Jim Fawcett
CSE687 – Object Oriented Design