To access the contents, click the chapter and section titles.
Fast Track Visual C++ 6.0 Programming
(Publisher: John Wiley & Sons, Inc.)
Author(s): Steve Holzner
ISBN: 0471312908
Publication Date: 09/01/98
Chapter 9 Multitasking Programming
In this chapter, we investigate multithreaded programs. Threads are execution streams and form the basis of multitasking. That is, each program that runs in Windows has its own main thread to launch the program, and it may start additional threads. Each new thread has its own procedure and executes the code in that procedure at the same time that other threads are working.
In this chapter, we support threads in Visual C++ programs. In particular, we learn to create the code to support additional threads in our programs, get those threads started, and let those threads communicate with the rest of the program.
In addition, once we support multiple threads, we can coordinate those threads. Each thread works on its own task in the background, and those tasks may be interrelated: One thread may have to wait until another is finished before continuing. We learn to coordinate multiple threads in this chapter as well.
The first step is to support a new thread in a program, which we do now.
Putting Threads to Work
To see how to support additional threads in a program, we set up an array with five integers.
array[0] = 0
array[1] = 1
array[2] = 2
array[3] = 3
array[4] = 4
Then we pass this array to a new thread and let that new thread find the maximum value in the array, which it passes back to the main thread. The main thread displays the result. We add a new item to the File menu, Find max, and the user selects that item to start the new thread.
Main Process
----------------
| |
| OnFileFindmax() |
| |
| |
| |
| |
| |
| |
| |
----------------
The code in OnFileFindmax() starts a new thread and calls the code in that threads main procedure, which we call ThreadProcedure().
Main Process New Thread
---------------- ------------------
| | | |
| OnFileFindmax() |---------->|ThreadProcedure() |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
---------------- -------------------
When this new thread completes its task, we need to inform the main thread. Because the new thread is a new and separate process, thats not as easy as it sounds. We communicate with the main process through a customized Windows message; when we send that message, a new function, OnMaxFound(), is called in the main process.
Main Process New Thread
---------------- ------------------
| | | |
| OnFileFindmax() |----------> |ThreadProcedure() |
| | | |
| | | |
| | Windows | |
| OnMaxFound() |<---------- | |
| | Message | |
| | | |
---------------- ------------------
In OnMaxFound(), we display the result of the new threads work in the view.
------------------------------------------------------
| |
|------------------------------------------------------ |
| ----------------------------- |
| |Max of the array = 4 | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| ----------------------------- |
| |
| |
------------------------------------------------------
Now that we know how this program works, lets write the code.
Creating the Threads Program
Create a new MDI program named Threads and add a new item to the File menu: Find max. When the user selects this menu item, we let the new thread find the maximum value in the array. To pass the array of data to the thread, we put that array into a data structure named Sdata.
// ThreadsView.h : interface of the CThreadsView class
//
/////////////////////////////////////////////////////////////////////////////
#if !defined(AFX_THREADSVIEW_H__A880E54F_A6EB_11D1_887F_D42B07C10710__INCLUDED_)
#define AFX_THREADSVIEW_H__A880E54F_A6EB_11D1_887F_D42B07C10710__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
struct SData
{
int array[5]; ⇐
.
.
.
};
In addition, we set aside space to store the maximum value in the array, when we find it, in an integer named max.
struct SData
{
int array[5];
int max; ⇐
};
We also create an object of this new data structure type and name that object data. (In C++, data structures create object just as classes do.)
class CThreadsView : public CView
{
protected: // create from serialization only
CThreadsView();
DECLARE_DYNCREATE(CThreadsView)
// Attributes
public:
CThreadsDoc* GetDocument();
SData data; ⇐
.
.
.
In the view classs OnFilemax() function, called when the user selects the Find max menu item, we fill the array in SData with data and initialize the integer weve named max to 0.
void CThreadsView::OnFileFindmax()
{
data.array[0] = 0; ⇐
data.array[1] = 1; ⇐
data.array[2] = 2; ⇐
data.array[3] = 3; ⇐
data.array[4] = 4; ⇐
data.max = 0; ⇐
.
.
.
|