Brief Full Advanced Search Search Tips
|  |  | 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 Search this book:
Previous | Table of Contents | Next
### Coordinating Multiple Threads Let’s 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 Threads In 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 view—WM_MIN—which 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. Let’s create the code for this project now. #### Creating the DoubleThreads Program Use AppWizard to create a new MDI project now named DoubleThreads. We use two new Windows messages here—WM_MAX and WM_MIN—so 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 we’ll call min, in a new member of our SData structure, SData, in the view’s 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 we’ll 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 we’re ready to launch the two threads.
Previous | Table of Contents | Next

| 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.