LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Problem:Passing arguments to thread function in C (https://www.linuxquestions.org/questions/programming-9/problem-passing-arguments-to-thread-function-in-c-948852/)

Thodoris21 06-06-2012 12:44 PM

Problem:Passing arguments to thread function in C
 
Hi.I want to convert a serial programm to parallel using posix threads in C.The program has a lot of variables.My problem is how pass many arguments/parameters using pthread_create.I used structs but it didn't work with all the variables.

I would appreciate if you could help me.
Thank you.

414N 06-06-2012 01:51 PM

Are you sure that the algorithm used by your program can be successfully made parallel?
Keep in mind that the performance gains obtainable (if any) via threads come at a cost: your code becomes more complex (way more complex if you have a lot of "private" info that needs to be passed to threads) and, if the refactoring is not carefully designed, you may introduce a whole lot of new difficult to spot bugs.

Thodoris21 06-06-2012 01:56 PM

It's an exercise for my university.I have made the serial program and it works fine.I have to make a parallel using posix threads to do the same job as serial but in less time.Serial program has a lot of private variables and i have problem passing them all in thread function.I will post the code of serial program but it's about 300 lines...

414N 06-06-2012 02:11 PM

I don't know how your program is laid out but can't you put the data needed for the threads in global variables?
Remember that threads within the same process share address space, file pointers and a lot of other things.

Thodoris21 06-06-2012 02:15 PM

I put the data in global variables but the compiler show errors.For example i have a variable which the the value from argv[1].How i can put it in global variable?

414N 06-06-2012 03:02 PM

I think you can copy its value into a global variable of the same type before threads creation...

Thodoris21 06-06-2012 03:03 PM

Do you give an example of this?

414N 06-06-2012 03:17 PM

Here's a simple example for an integer global variable:
Code:

int intArg;
...
int main (int argc, char *argv[]){
...
intArg = atoi(argv[1]);
...
pthread_create(...);
...
exit 0;
}

If the value to be externalized in the global scope is a string, then you'd have to make sure that the correct amount of memory is allocated before a strcpy.
It's kinda natural that the responsibility for variable initialization lies in a section of your program which gets run before thread creation.

Thodoris21 06-06-2012 03:19 PM

Does it work fine?

414N 06-06-2012 03:22 PM

Try it with some of your variables.

bigearsbilly 06-06-2012 05:18 PM

Quote:

Originally Posted by Thodoris21 (Post 4697107)
using posix threads to do the same job as serial but in less time.

Prepare to be disappointed! Unless this is an exercise you would probably be better employed doing something else.

Thodoris21 06-07-2012 01:18 AM

I must do the exercise to pass the exams!

Thodoris21 06-07-2012 01:33 AM

Quote:

Originally Posted by 414N (Post 4697173)
Here's a simple example for an integer global variable:
Code:

int intArg;
...
int main (int argc, char *argv[]){
...
intArg = atoi(argv[1]);
...
pthread_create(...);
...
exit 0;
}

If the value to be externalized in the global scope is a string, then you'd have to make sure that the correct amount of memory is allocated before a strcpy.
It's kinda natural that the responsibility for variable initialization lies in a section of your program which gets run before thread creation.

It doesn't work.Segmentation fault!

pan64 06-07-2012 01:49 AM

there are several things which can produce a segmentation fault in a multithreaded environment.
You would probably try to build your code in debug mode and view the stack trace or debug the code to see what's happened. But at least you need to give us more info "it does not work" is not enough. We do not know what have you tried exactly, how, and what's happened....

Thodoris21 06-07-2012 01:54 AM

I try to make argv global variables...
Code:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

int dim;
int threads;

void *maps(void *id) {

  int thread_id = *((int*)id);
  int from = (dim/threads*thread_id);
  int to = (dim/threads*thread_id)+dim/threads;
     
  printf("Map dimensions %dx%d\n",dim,dim);
}


int main (int argc, char *argv[]) {
  int i;
  dim=atoi(argv[1]);
  threads=atoi(argv[2]);
  pthread_t tid[threads];

  for (i = 0; i < threads; i++) {
        pthread_create(&tid[i], NULL, &maps,&i);
  }

  for (i = 0; i < threads; i++) {
          pthread_join(tid[i], NULL);
  }       
   

return 0;
}



All times are GMT -5. The time now is 06:48 PM.