home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / powergui / thread / exit / threadex.cpp < prev   
Encoding:
C/C++ Source or Header  |  1996-10-29  |  3.4 KB  |  122 lines

  1. /*******************************************************************************
  2. * FILE NAME: threadex.cpp                                                      *
  3. *                                                                              *
  4. * DESCRIPTION:                                                                 *
  5. *   This sample demonstrates effects of the various techniques of thread       *
  6. *   termination.                                                               *
  7. *                                                                              *
  8. * COPYRIGHT:                                                                   *
  9. *   Licensed Materials - Property of Solution Frameworks                       *
  10. *   Copyright (C) 1996, Solution Frameworks                                    *
  11. *   All Rights Reserved                                                        *
  12. *******************************************************************************/
  13. #ifdef __OS2__
  14.   #define INCL_DOSPROCESS
  15.   #include <os2.h>
  16.   #define OSExit( rc ) DosExit( EXIT_THREAD, rc )
  17. #else
  18.   #include <windows.h>
  19.   #define OSExit( rc ) ExitThread( rc )
  20. #endif
  21.  
  22. #include <iostream.h>
  23.  
  24. #include <stdlib.h>
  25.  
  26. #include <ithread.hpp>
  27.  
  28. #include <istring.hpp>
  29.  
  30. struct ReportDestruction {
  31.   ReportDestruction ( const char *id )
  32.     : id( id ) {
  33.     }
  34.   ~ReportDestruction ( ) {
  35.     cout << id << " destroyed." << endl;
  36.   }
  37. const char
  38.  *id;
  39. };
  40.  
  41. void _Optlink normalExit( void *p ) {
  42.   ReportDestruction
  43.     object( "Stack based object" );
  44.   cout << IString( (char*)p ) 
  45.        << " exiting normally via return" 
  46.        << endl;
  47.   return;
  48. }
  49.  
  50. void _Optlink osExit( void *p ) {
  51.   ReportDestruction
  52.     object( "Stack based object" );
  53.   cout << IString( (char*)p ) 
  54.        << " exiting via OS \"exit\"" 
  55.        << endl;
  56.   OSExit( 0 );
  57. }
  58.  
  59. void _Optlink cLibExit( void *p ) {
  60.   ReportDestruction
  61.     object( "Stack based object" );
  62.   cout << IString( (char*)p )
  63.        << " exiting via _endthread" 
  64.        << endl;
  65.   _endthread();
  66. }
  67.  
  68. void _Optlink iThreadExit( void *p ) {
  69.   ReportDestruction
  70.     object( "Stack based object" );
  71.   cout << IString( (char*)p )
  72.        << " exiting via IThread::exit" 
  73.        << endl;
  74.   IThread::current().exit(0);
  75. }
  76.  
  77. ReportDestruction
  78.   staticObject( "Global Static" );
  79.  
  80. void main( int, char *argv[] ) {
  81.   ReportDestruction
  82.     object( "Stack based object on primary thread" );
  83.  
  84.   IThread
  85.     thread2( normalExit, "Thread 2" );
  86.   IThread::current().waitFor( thread2 );
  87.  
  88.   IThread
  89.     thread3( osExit, "Thread 3" );
  90.   IThread::current().waitFor( thread3 );
  91.  
  92.   IThread
  93.     thread4( cLibExit, "Thread 4" );
  94.   IThread::current().waitFor( thread4 );
  95.  
  96.   IThread
  97.     thread5( iThreadExit, "Thread 5" );
  98.   IThread::current().waitFor( thread5 );
  99.  
  100.   IString
  101.     exitMode( argv[1] );
  102.  
  103.   if ( exitMode == "" ) {
  104.     exitMode = "normal";
  105.   }
  106.  
  107.   if ( exitMode == "os" ) {
  108.     cout << "Exiting primary thread via OS \"exit\"" << endl;
  109.     OSExit( 0 );
  110.   } else if ( exitMode == "endthread" ) {
  111.     cout << "Exiting primary thread via _endthread" << endl;
  112.     _endthread();
  113.   } else if ( exitMode == "ithread" ) {
  114.     cout << "Exiting primary thread via IThread::exit" << endl;
  115.     IThread::current().exit(0);
  116.   } else /* "normal" */ {
  117.     cout << "Exiting primary thread normally via return" << endl;
  118.   }
  119.  
  120.   return;
  121. }
  122.