mrmurmanks
mrmurmanks

Reputation: 93

Can I use pthreads over the same function on C?

I have a doubt that might be silly guys. I am having a function to calculate some mathematical formulas as an example.

# include <stdio.h>
# include <time.h>
# include <stdlib.h>
# include <pthread.h>
# include <unistd.h>
# include <math.h>


pthread_mutex_t a_mutex = PTHREAD_MUTEX_INITIALIZER;
volatile long int a = 0;


void threadOne(void *arg) 
{


 int i;
    long int localA = 0;

    for (i = 1; i < 50000000; i++) 
    {
     localA = localA + i*a*sqrt(a);
    }

    pthread_mutex_lock(&a_mutex);
    a = a + localA;
    pthread_mutex_unlock(&a_mutex);
}


void threadTwo(void *arg) 
{
    int i;
    long int localA = 0;
    for (i = 50000000; i <= 100000000; i++)
    {
     localA = localA + i*a*sqrt(a);
    }
   pthread_mutex_lock(&a_mutex);
   a = a + localA;
   pthread_mutex_unlock(&a_mutex);
}


int main (int argc, char **argv) 
{
    pthread_t one, two;
    int i;
    pthread_create(&one, NULL, (void*)&threadOne, NULL);
    pthread_create(&two, NULL, (void*)&threadTwo, NULL);
    pthread_join(one, NULL);
    pthread_join(two, NULL);
}

Now this is an example I found, I am having two functions with a thread each one, so one is calculated on a different thread. But can I have just one function and then have two threads to one function, so the function runs twice with different data?. My idea is this one: I am having just one function that can have two different sets of data, then the function can run with the first set or the second set depending on the thread is running.

But is this possible even?. I want to avoid something as copying the function twice as here.

Lets use say that I only keep the function

void threadOne(void *arg)

But I run it twice using different threads at same time with different data, this can be achieved or I am just being silly?.

Upvotes: 1

Views: 407

Answers (1)

dbush
dbush

Reputation: 223699

Yes, this can be done by making use of the argument to the thread function.

Each thread needs to loop over a range of values. So create a struct definition to contain the min and max values:

struct args {
    int min;
    int max;
};

Define a single thread function which converts the void * argument to a pointer to this type and reads it:

void *thread_func(void *arg) 
{
    struct args *myargs = arg;
    int i;
    long int localA = 0;
    for (i = myargs->min; i < myargs->max; i++) 
    {
        localA = localA + i*a*sqrt(a);
    }
    pthread_mutex_lock(&a_mutex);
    a = a + localA;
    pthread_mutex_unlock(&a_mutex);
    return NULL;
}

(Note that the function needs to return a void * to conform to the interface pthread_create expects.)

Then in your main function create an instance of this struct for each set of arguments, and pass that to pthread_create:

int main (int argc, char **argv) 
{
    pthread_t one, two;
    struct args args1 = { 1, 50000000 };
    struct args args2 = { 50000000 , 100000000 };
    pthread_create(&one, NULL, thread_func, &args1);
    pthread_create(&two, NULL, thread_func, &args2);
    pthread_join(one, NULL);
    pthread_join(two, NULL);
}

Upvotes: 2

Related Questions