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. |
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. |
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...
|
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. |
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?
|
I think you can copy its value into a global variable of the same type before threads creation...
|
Do you give an example of this?
|
Here's a simple example for an integer global variable:
Code:
int intArg; It's kinda natural that the responsibility for variable initialization lies in a section of your program which gets run before thread creation. |
Does it work fine?
|
Try it with some of your variables.
|
Quote:
|
I must do the exercise to pass the exams!
|
Quote:
|
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.... |
I try to make argv global variables...
Code:
#include <stdio.h> |
All times are GMT -5. The time now is 06:48 PM. |