home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / powergui / thread / improved / easy.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-29  |  1.8 KB  |  60 lines

  1. /***************************************************************
  2. * FILE NAME: easy.cpp                                          *
  3. *                                                              *
  4. * DESCRIPTION:                                                 *
  5. *   Sample program demonstrating use of simplified             *
  6. *   "threadFn" template function to generate an                *
  7. *   IThreadMemberFn object.                                    *
  8. *                                                              *
  9. * COPYRIGHT:                                                   *
  10. *   Licensed Materials - Property of Solution Frameworks       *
  11. *   Copyright (C) 1996, Solution Frameworks                    *
  12. *   All Rights Reserved                                        *
  13. ***************************************************************/
  14. #include <iostream.h>
  15.  
  16. #include <istring.hpp>
  17.  
  18. #include "improved.hpp"
  19.  
  20. const char
  21.   theHardWay[] = "using \"new IThreadMemberFn<MyClass>"
  22.                           "( myObject, MyClass::foo )\"",
  23.   theEasyWay[] = "using \"threadFn( myObject, MyClass::bar )\"";
  24.  
  25. // Here's a typicall C++ class.  It has two member functions
  26. // that we'll run on a separate thread.
  27. struct MyClass {
  28. void foo ( ) {
  29.   cout << "I was started " << theHardWay << endl;
  30. }
  31. void bar ( ) {
  32.   cout << "I was started " << theEasyWay << endl;
  33. }
  34. };
  35.  
  36. void main()
  37.   {
  38.   // An object of that class.
  39.   MyClass
  40.     myObject;
  41.  
  42.   // Start MyClass::foo the hard way.
  43.   IThread
  44.     hard;
  45.  
  46.   hard.start( new IThreadMemberFn<MyClass>( myObject, MyClass::foo ) );
  47.  
  48.   // Start MyClass::bar the easy way.
  49.   IThread
  50.     easy;
  51.  
  52.   easy.start( threadFn( myObject, MyClass::bar ) );
  53.  
  54. #ifdef IC_PM
  55.   IThread::current().waitForAllThreads();
  56. #else
  57.   IThread::current().sleep( 1000 );
  58. #endif
  59.   }
  60.