Dear all,
When one has a class,
class Q {
int s;
type *ptr; //array with s elements.
public:
//constructors, assignment operator
void gg(int *sum, int sz);
//destructor
};
and want to use pthread_create() to create multiple threads that
execute the function gg() on different elements of the array pointed
to by ptr, what's the correct way to go about it?
Suppose gg() is defined as:
void gg(int *sum, int sz){
for (int j = 0; j < sz; j++)
sum[j] = ptr[j] + A; // A is a constant
}
The signature of pthread_create() is:
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void
*(*routine)(void *), void *arg) ,
where routine is the function to be run by thread and arg is the
argument for routine.
In my main() I want to do:
int main(){
pthread_t mythreads[N]; //N integer > 1
int ids[N];
for (int j = 0; j < N; j++)
ids[j] = pthread_create(&mythreads[j], NULL, /* the problem is
here*/);
for (int j = 0; j < N; j++)
pthread_join(mythreads[j], ...); //dont worry here
return 0;
}
/*the problem is here*/ embedded comment: how to (re)define gg() and
its arguments (s and *ptr) to be the third and fourth arguments of
pthread_create().
I know nothing of Boost, therefore I don't know whether it makes this
sort of things easier.
Thank you.
--
[ See http://www.gotw.ca/resources/clcm.htm
for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


|