Thread View: comp.sys.next.programmer
1 messages
1 total messages
Started by Anne Wilson
Fri, 31 Jul 1992 16:17
invoking C++ member functions via cthread_fork()
Author: Anne Wilson
Date: Fri, 31 Jul 1992 16:17
Date: Fri, 31 Jul 1992 16:17
72 lines
2325 bytes
2325 bytes
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);
}
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads