home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!gatech!rpi!zaphod.mps.ohio-state.edu!darwin.sura.net!dtix!mimsy!wilson
- From: wilson@mimsy.umd.edu (Anne Wilson)
- Newsgroups: comp.sys.next.programmer
- Subject: invoking C++ member functions via cthread_fork()
- Keywords: member function, this, cthread_fork
- Message-ID: <59354@mimsy.umd.edu>
- Date: 31 Jul 92 16:17:15 GMT
- Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742
- Lines: 72
-
- Has anyone successfully invoked an object's member function via
- cthread_fork() and passed it an argument in addition to the
- 'this' pointer? I have successfully invoked a member function
- passing 'this' as the first argument, but am unable to have the
- member function receive the additional argument. I have tried
- many permutations of the code below (including rearranging the
- order of the struct's members), but nothing seems to work. One
- alternative is to pass data by adding another data member to the
- class. This works, but it's not very satisfying.
-
- Thanks in advance! Please email to me directly, if possible.
-
- Anne
- wilson@cs.umd.edu
-
- ////----------------------------------------------------------
- extern "C" {
- #import <stdio.h>
- #import <cthreads.h>
- }
-
- class B {
- protected:
- int data;
- public:
- B (int initval);
- };
-
- B::B (int initval) {
- data = initval;
- }
-
- //------------------------------------------
- typedef void (viP_func_ptr)(void*, int);
-
- class D : public B {
- public:
- D(int);
- void addTo(int);
- void startThread(viP_func_ptr memberFn, int val);
- };
-
- typedef struct{
- void* me;
- int val1;
- } argthing;
-
- static argthing myArgs;
-
- void D::startThread(viP_func_ptr* memberFn, int val){
- myArgs.me = this;
- myArgs.val1 = val;
-
- /* must pass 'this' to a member fn called via cthread_fork -
- * C++ converts member fns to C fns with 'this' as first arg */
- cthread_detach(cthread_fork((cthread_fn_t) memberFn, (any_t)myArgs.me));
- sleep(1);
- }
- void D::addTo(int val) {
- printf (">>>>>>>>>In D::addTo, this, val = %d, %d\n", (int) this, val);
- data += *val;
- }
- D::D(int val) : B (val) {
- printf ("in Constructor: this = %d\n", (int) this);
- startThread((void*) addTo, 10);
- printf ("After call to startThread\n");
- }
-
- main () {
-
- D myD(20);
- }
-