0

is it possible to pass an already defined function into a pthread?

so for example within my code i am able to define an array size once a user enters an array size(for example user enters 200) it will add the numbers 1-200 to an array , i then have a function that will print the array data into a textfile then another function that will print the array in reverse to the same text file.

I want to have two pthreads that will handle the printArray function and another pthread to handle the printArrayReverse function.

so here is the function for the printArray

void printArray (int size, int a1[] )
{

cout << " Array in order " << endl;
for( int i=0; i< size; ++i)
{
cout << a1[i] << " ";
cout << endl;
cout << "----------------"<<endl;
writeToText(a1[i]);

}

so within my int main

i have my two threads

pthread_t threadOne, threadTwo;

then i want the printArray function to be in this threadOne then my print reverse in threadTwo.

pthread_create(&threadOne, NULL , &thread_function, NULL);
pthread_create(&threadTwo, NULL , &thread_function, NULL);

pthread_join(threadOne,NULL)
pthread_join(threadTwo,NULL)

So basically is it possible to say that if i created a new function called

void* thread_function ()
    {

    }

could i call the printArray into there?

2
Contributors
2
Replies
3
Views
4 Years
Discussion Span
Last Post by FakeTales
Featured Replies
  • 1

    The signature of the function `pthread_create` expects to call is `void * function(void *)` and the fourth argument is a `void*` that is the argument passed to that function on thread creation. So, for example: void * printFunction(void * arg) { int * array = (int *)arg; // ... } … Read More

1

The signature of the function pthread_create expects to call is void * function(void *) and the fourth argument is a void* that is the argument passed to that function on thread creation. So, for example:

void * printFunction(void * arg) {
   int * array = (int *)arg;
   // ...
}

int main () {
   int a[] = { 1, 2, 3, 4, 5 };
   pthread_t thread;
   pthread_create (&thread, NULL, printFunction, (void *)a);
   //...
}

Can be though of as calling printFunction directly as printFunction (a) except the execution happens in a seperate thread.

0

The signature of the function pthread_create expects to call is void * function(void ) and the fourth argument is a void that is the argument passed to that function on thread creation. So, for example:

  void * printFunction(void * arg) {
       int * array = (int *)arg;
       // ...
    }
    int main () {
       int a[] = { 1, 2, 3, 4, 5 };
       pthread_t thread;
       pthread_create (&thread, NULL, printFunction, (void *)a);
       //...
    }

Can be though of as calling printFunction directly as printFunction (a) except the execution happens in a seperate thread.

Thank you L7Sqr from your answer i was able to do this

void * threadOne_function (void * arg)
{
int s1=size;
int *a  = (int *)arg;
cout << "Thread One " << endl;
for( int i=0; i< s1; ++i)

{
cout << a[i] << " ";
cout << endl;
writeToText(a[i]);

}
}
This question has already been answered. Start a new discussion instead.
Please be thoughtful and detailed and be sure to adhere to our posting rules.