TIP: Click on subject to list as thread! ANSI
echo: os2prog
to: Ruud Senden
from: Jonathan de Boyne Pollard
date: 1994-12-27 05:05:44
subject: Threads and classes

//
  //  ********************************************************************
  //   A small thread class, showing how threads can be encapsulated as
  //   class instances, and showing how to use a member function as the
  //   main function of a thread.
  //  ********************************************************************
  //
  //  (c) Copyright 1994 Jonathan de Boyne Pollard.  All rights reserved.
  //              Jonathan de Boyne Pollard, FIDONET 2:440/4.0
  //
  //  Permission is hereby granted to post, steal and abuse this code
  //  for your own purposes to your heart's content, as long as you
  //  realise that I take no responsibility whatsoever for what it does
  //  to your machine, data, cat, or marital status.
  //

  class Thread {
  protected:
       virtual void main ( void ) = 0 ;
       void begin ( unsigned long ) ;
  } ;

  static
  void
  run ( void * v )
  {
      ((Thread *)v)->main() ;
  }

  void
  Thread::begin ( unsigned long stack )
  {
       _beginthread(run, stack, this) ;
  }

  class MyThread : public Thread {
        int per_thread_data1 ;
        char per_thread_data2 ;

        virtual void main ( void ) ;
  public:
        MyThread ( int i, char c ) :
             per_thread_data1(i),
             per_thread_data2(c)
          {
             begin(4096*64) ;
          }
  } ;

  void
  MyThread::main ( void )
  {
       //
       //  Put the main code of the thread here.  The instance data
       //  members of the class are "per-thread" data.
       //
  }

  int
  main ( int, char ** )
  {
      MyThread thread2(1, 'g') ;

      //
      //   Do other processing in the main thread
      //
  }

  //
  //  Of course, the idea is extensible.  If another thread needs to
  //  communicate with an instance of the MyThread class, for example, you
  //  can write a public member function to do it and initialise all
  //  relevant semaphores and suchlike in the constructor.
  //

  > JdeBP <
___
 X MegaMail 2.10 #0:
--- Maximus/2 2.02
* Origin: DoNoR/2,Woking UK (01483-725167) (2:440/4)
SEEN-BY: 12/2442 620/243 624/50 632/348 640/820 690/660 711/409 410 413 430
SEEN-BY: 711/807 808 809 934 942 949 712/353 515 713/888 800/1
@PATH: 440/4 141/209 270/101 396/1 3615/50 229/2 12/2442 711/409 808 809 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.