home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / advanced / mutexes / threads.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  4.5 KB  |  191 lines

  1. // threads.cpp : implementation file
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12. //
  13. // This sample application is derived from the Mutex application
  14. // distributed with Jeffrey Richter's "Advanced Windows" programming book
  15. // (Microsoft Press).  See the book for more information about Win32
  16. // programming topics, including thread synchronization.
  17.  
  18. #include "stdafx.h"
  19. #include "mutexes.h"
  20. #include "mutexdlg.h"
  21. #include "threads.h"
  22.  
  23. #ifdef _DEBUG
  24. #undef THIS_FILE
  25. static char THIS_FILE[] = __FILE__;
  26. #endif
  27.  
  28. IMPLEMENT_DYNCREATE(CExampleThread, CWinThread)
  29.  
  30. CExampleThread::CExampleThread()
  31. {
  32.     m_bDone = FALSE;
  33.     m_pOwner = NULL;
  34.     m_bAutoDelete = FALSE;
  35. }
  36.  
  37. /////////////////////////////////////////////////////////////////////////////
  38. // CCounterThread
  39.  
  40. IMPLEMENT_DYNCREATE(CCounterThread, CExampleThread)
  41.  
  42. CCounterThread::CCounterThread()
  43. {
  44. }
  45.  
  46. CCounterThread::~CCounterThread()
  47. {
  48. }
  49.  
  50. BOOL CCounterThread::InitInstance()
  51. {
  52.     // TODO:  perform and per-thread initialization here
  53.     return TRUE;
  54. }
  55.  
  56. int CCounterThread::Run()
  57. {
  58.     BOOL fSyncChecked;
  59.     unsigned int nNumber;
  60.  
  61.     if (m_pOwner == NULL)
  62.         return -1;
  63.  
  64.     CSingleLock sLock(&(m_pOwner->m_mutex));
  65.  
  66.     while (!m_bDone)
  67.     {
  68.         // Get the status of the Synchronize
  69.         // check box and save it.
  70.  
  71.         fSyncChecked = m_pOwner->IsDlgButtonChecked(IDC_SYNCHRONIZE);
  72.  
  73.         // If the user wants us synchronized, do it!
  74.         if (fSyncChecked)
  75.             sLock.Lock();
  76.  
  77.         _stscanf((LPCTSTR) m_pOwner->m_strNumber, _T("%d"), &nNumber);
  78.         nNumber++;
  79.  
  80.         m_pOwner->m_strNumber.Empty();
  81.         while (nNumber != 0)
  82.         {
  83.             m_pOwner->m_strNumber += (TCHAR) ('0' + nNumber%10);
  84.  
  85.             // A call to Sleep() here tells the system that we want
  86.             // to relinquis the remainder of our time slice to
  87.             // another thread.  this call is needed for
  88.             // single-CPU systems so that the results of the
  89.             // synchronization or lack thereof are obvious.
  90.             // Normally, your programs would NOT call Sleep() here.
  91.  
  92.             Sleep(0);
  93.  
  94.             // get ready to get the next digit.
  95.             nNumber /= 10;
  96.         }
  97.  
  98.         // Characters were generated in reverse order,
  99.         // reverse the string
  100.         m_pOwner->m_strNumber.MakeReverse();
  101.  
  102.         if (fSyncChecked)
  103.         {
  104.         // If the user wants synchronization, do it.
  105.             sLock.Unlock();
  106.         }
  107.  
  108.         // If the user wants to display something
  109.         // after each iteration, do it.
  110.  
  111.         if (m_pOwner->IsDlgButtonChecked(IDC_SHOWCNTRTHRD))
  112.             m_pOwner->AddToListBox(_T("Cntr: Increment"));
  113.     }
  114.  
  115.     m_pOwner->PostMessage(WM_CLOSE, 0, 0L);
  116.     return 0;
  117. }
  118.  
  119. BEGIN_MESSAGE_MAP(CCounterThread, CWinThread)
  120.     //{{AFX_MSG_MAP(CCounterThread)
  121.         // NOTE - the ClassWizard will add and remove mapping macros here.
  122.     //}}AFX_MSG_MAP
  123. END_MESSAGE_MAP()
  124.  
  125. /////////////////////////////////////////////////////////////////////////////
  126. // CCounterThread message handlers
  127. /////////////////////////////////////////////////////////////////////////////
  128. // CDisplayThread
  129.  
  130. IMPLEMENT_DYNCREATE(CDisplayThread, CExampleThread)
  131.  
  132. CDisplayThread::CDisplayThread()
  133. {
  134. }
  135.  
  136. CDisplayThread::~CDisplayThread()
  137. {
  138. }
  139.  
  140. BOOL CDisplayThread::InitInstance()
  141. {
  142.     // TODO:  perform and per-thread initialization here
  143.     return TRUE;
  144. }
  145.  
  146. int CDisplayThread::Run()
  147. {
  148.     BOOL fSyncChecked;
  149.     CString strBuffer;
  150.  
  151.     ASSERT(m_pOwner != NULL);
  152.     if (m_pOwner == NULL)
  153.         return -1;
  154.  
  155.     CSingleLock sLock(&(m_pOwner->m_mutex));
  156.  
  157.     while (!m_bDone)
  158.     {
  159.         // Determine whether the user wants the threads
  160.         // to be synchronized.
  161.  
  162.         fSyncChecked = m_pOwner->IsDlgButtonChecked(IDC_SYNCHRONIZE);
  163.  
  164.         if (fSyncChecked)
  165.             sLock.Lock();
  166.  
  167.         // Construct a string with the string form of the number.
  168.  
  169.         strBuffer = _T("Dspy: ");
  170.         strBuffer += m_pOwner->m_strNumber;
  171.  
  172.         if (fSyncChecked)
  173.             sLock.Unlock();
  174.  
  175.         // Add the string form of the number to the list box.
  176.         m_pOwner->AddToListBox(strBuffer);
  177.     }
  178.  
  179.     m_pOwner->PostMessage(WM_CLOSE, 0, 0L);
  180.     return 0;
  181. }
  182.  
  183. BEGIN_MESSAGE_MAP(CDisplayThread, CWinThread)
  184.     //{{AFX_MSG_MAP(CDisplayThread)
  185.         // NOTE - the ClassWizard will add and remove mapping macros here.
  186.     //}}AFX_MSG_MAP
  187. END_MESSAGE_MAP()
  188.  
  189. /////////////////////////////////////////////////////////////////////////////
  190. // CDisplayThread message handlers
  191.