ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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.
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....
#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;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.