home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / sys / next / programm / 5388 < prev    next >
Encoding:
Text File  |  1992-07-31  |  2.2 KB  |  83 lines

  1. Path: sparky!uunet!gatech!rpi!zaphod.mps.ohio-state.edu!darwin.sura.net!dtix!mimsy!wilson
  2. From: wilson@mimsy.umd.edu (Anne Wilson)
  3. Newsgroups: comp.sys.next.programmer
  4. Subject: invoking C++ member functions via cthread_fork()
  5. Keywords: member function, this, cthread_fork
  6. Message-ID: <59354@mimsy.umd.edu>
  7. Date: 31 Jul 92 16:17:15 GMT
  8. Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742
  9. Lines: 72
  10.  
  11. Has anyone successfully invoked an object's member function via 
  12. cthread_fork() and passed it an argument in addition to the 
  13. 'this' pointer?  I have successfully invoked a member function 
  14. passing 'this' as the first argument, but am unable to have the 
  15. member function receive the additional argument.  I have tried 
  16. many permutations of the code below (including rearranging the 
  17. order of the struct's members), but nothing seems to work.  One 
  18. alternative is to pass data by adding another data member to the 
  19. class.  This works, but it's not very satisfying.
  20.  
  21. Thanks in advance!  Please email to me directly, if possible.
  22.  
  23. Anne
  24. wilson@cs.umd.edu
  25.  
  26. ////----------------------------------------------------------
  27. extern "C" {
  28. #import <stdio.h>
  29. #import <cthreads.h>
  30. }
  31.  
  32. class B {
  33. protected:
  34.     int data;
  35. public:
  36.     B (int initval);
  37. };
  38.  
  39. B::B (int initval) { 
  40.   data = initval;
  41. }
  42.  
  43. //------------------------------------------
  44. typedef void (viP_func_ptr)(void*, int);                    
  45.  
  46. class D : public B {
  47. public:
  48.     D(int);
  49.     void addTo(int);
  50.     void startThread(viP_func_ptr memberFn, int val);
  51. };
  52.  
  53. typedef struct{
  54.     void* me;
  55.     int val1;
  56. } argthing;
  57.  
  58. static argthing myArgs;
  59.  
  60. void D::startThread(viP_func_ptr* memberFn, int val){
  61.     myArgs.me = this;
  62.     myArgs.val1 = val;
  63.     
  64.     /* must pass 'this' to a member fn called via cthread_fork - 
  65.      * C++ converts member fns to C fns with 'this' as first arg */
  66.     cthread_detach(cthread_fork((cthread_fn_t) memberFn, (any_t)myArgs.me));
  67.     sleep(1);
  68. }
  69. void D::addTo(int val) {
  70.     printf (">>>>>>>>>In D::addTo, this, val = %d, %d\n", (int) this, val);
  71.     data += *val;
  72. }
  73. D::D(int val) : B (val) {
  74.     printf ("in Constructor: this = %d\n", (int) this);
  75.     startThread((void*) addTo, 10);
  76.     printf ("After call to startThread\n");
  77. }
  78.  
  79. main () {
  80.  
  81.     D myD(20);
  82. }
  83.