![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Fast Track Visual C++ 6.0 Programming
Coordinating Multiple ThreadsLets say that we have two threads in a program and that we want to coordinate them in some way. This is a frequent problem in multithreaded programs if you have one resource, such as a disk file, that a number of threads work with: One thread may have to complete its job before another thread can work with the resource. In this next example, we support and coordinate two threads. One thread finds the maximum value in the array in our data structure, and the other finds the minimum. To coordinate those threads, we use the Windows SetEvent() and ResetEvent() functions. Using Events to Coordinate ThreadsIn our new program, DoubleThreads, we set up two threads: MaxThread and MinThread. When the user clicks the Find max and min item in the File menu, we start the MaxThread thread, which finds the maximum value in the array. We also start the second thread, MinThread, but that thread first calls a function named WaitForSingleObject(), which makes it wait for a certain event to be set. This means that MinThread does nothing while MaxThread is doing its work. Main Process MaxThread -------------------- --------------------- | | | | |OnFileFindmaxandmin()|----------> |MaxThreadProcedure() | | | | | | | | | | | | | | OnMaxFound() | | | | | | | | | | | | | | | | | | | | | --------------------- | | | | MinThread | | --------------------- | | | | | | |MinThreadProcedure() | | | |{ | | | |WaitForSingleObject()| | | |} | | | | | | | | | | | | | | | | | | | | | --------------------- --------------------- When the maximum value in the array is found, we set the event that MinThread is waiting for. This means that MinThread can continue, finding the minimum in the array and sending a new Windows message back to the viewWM_MINwhich makes the view call a new function, OnMinFound(). Main Process MaxThread ---------------------- -------------------- | | | | |OnFileFindmaxandmin() |----------> |MaxThreadProcedure() | | | | | | | | | | | | | | | Windows | | | | OnMaxFound() |<---------- | | | | Message | | | | | | | | ------- |SetEvent() | | | | | | | | | --------------------- | | | | | | MinThread | | | --------------------- | | | | | | | | |MinThreadProcedure() | | | | |{ | | | ------>|WaitForSingleObject() | | | | . | | | | . | | | | . | | | | | | | Windows | | | OnMinFound() |<---------- | | | | Message |} | | | | | --------------------- --------------------- In this way, the second thread is coordinated with the first, waiting for the first thread to complete its work before going on. Lets create the code for this project now. Creating the DoubleThreads ProgramUse AppWizard to create a new MDI project now named DoubleThreads. We use two new Windows messages hereWM_MAX and WM_MINso we add those definitions to the DoubleThread.h file. // DoubleThreads.h : main header file for the DOUBLETHREADS application // #if !defined(AFX_DOUBLETHREADS_H__A880E55B_A6EB_11D1_887F_D42B07C10710__INCLUDED_) #define AFX_DOUBLETHREADS_H__A880E55B_A6EB_11D1_887F_D42B07C10710__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #ifndef __AFXWIN_H__ #error include stdafx.h before including this file for PCH #endif #include resource.h // main symbols #define WM_MAX (WM_USER + 1) ⇐ #define WM_MIN (WM_USER + 2) ⇐ . . . In addition, we need two thread procedures that we can name MaxTheadProcedure() and MinThreadProcedure(). // DoubleThreadsView.h : interface of the CDoubleThreadsView class // ///////////////////////////////////////////////////////////////////////////// #if !defined(AFX_DOUBLETHREADSVIEW_H__A880E565_A6EB_11D1_887F_D42B07C10710__INCLUDED_) #define AFX_DOUBLETHREADSVIEW_H__A880E565_A6EB_11D1_887F_D42B07C10710__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 UINT MaxThreadProcedure(LPVOID pParam); ⇐ UINT MinThreadProcedure(LPVOID pParam); ⇐ We also set aside storage space for the minimum of the array, which well call min, in a new member of our SData structure, SData, in the views header. struct SData { int array[5]; int max; int min; ⇐ HWND NotifyWindow; }; We then declare an object, named data, of this structure type, as well as the threads and thread procedures well need. class CDoubleThreadsView : public CView { protected: // create from serialization only CDoubleThreadsView(); DECLARE_DYNCREATE(CDoubleThreadsView) // Attributes public: CDoubleThreadsDoc* GetDocument(); SData data; ⇐ CWinThread* MaxThread; ⇐ void OnMaxFound(WPARAM wParam, LPARAM lParam); ⇐ CWinThread* MinThread; ⇐ void OnMinFound(WPARAM wParam, LPARAM lParam); ⇐ Now were ready to launch the two threads.
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |