Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > C++ and POSIX t...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 3 Topic 9537 of 9593
Post > Topic >>

C++ and POSIX threads

by mpho <tjabane@[EMAIL PROTECTED] > Apr 23, 2008 at 02:13 PM

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! ]




 3 Posts in Topic:
C++ and POSIX threads
mpho <tjabane@[EMAIL P  2008-04-23 14:13:19 
Re: C++ and POSIX threads
Martin York <Martin.Yo  2008-04-24 01:05:14 
Re: C++ and POSIX threads
Peter Jones <dev-null@  2008-04-24 01:43:18 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue May 13 13:14:28 CDT 2008.