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

  1. /***************************************************************
  2. * FILE NAME: broken.cpp                                        *
  3. *                                                              *
  4. * DESCRIPTION:                                                 *
  5. *   Sample program demonstrating problems with reference-      *
  6. *   counting of IThreadFn objects.                             *
  7. *                                                              *
  8. * COPYRIGHT:                                                   *
  9. *   Licensed Materials - Property of Solution Frameworks       *
  10. *   Copyright (C) 1996, Solution Frameworks                    *
  11. *   All Rights Reserved                                        *
  12. ***************************************************************/
  13. #include <iostream.h>
  14.  
  15. #include <ithread.hpp>
  16. #include <istring.hpp>
  17.  
  18. struct MyClass {
  19. void foo ( )
  20.   {
  21.   cout << "MyClass::foo called\n";
  22.   }
  23. };
  24.  
  25. void main()
  26.   {
  27.   MyClass
  28.     myObject;
  29.  
  30.   IThreadMemberFn<MyClass>
  31.     // Here's an IThreadFn object on the stack (seems reasonable).
  32.     memberFn1( myObject, MyClass::foo ),
  33.     // Here's an IThreadFn object allocated on the heap.
  34.    *memberFn2( new IThreadMemberFn<MyClass>( myObject, MyClass::foo ) );
  35.  
  36.   IThread
  37.     thread1, thread2;
  38.  
  39.   thread1.start( &memberFn1 ),
  40.   thread2.start( memberFn2 );
  41.  
  42. #ifdef IC_PM
  43.   IThread::current().waitForAllThreads();
  44. #else
  45.   IThread::current().sleep(1000);
  46. #endif
  47.  
  48.   // We have to delete the object we allocated, right?
  49.   delete memberFn2;
  50.   }
  51.