LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-06-2012, 12:44 PM   #1
Thodoris21
LQ Newbie
 
Registered: May 2012
Posts: 27

Rep: Reputation: Disabled
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.
 
Old 06-06-2012, 01:51 PM   #2
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 609

Rep: Reputation: 180Reputation: 180
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.
 
Old 06-06-2012, 01:56 PM   #3
Thodoris21
LQ Newbie
 
Registered: May 2012
Posts: 27

Original Poster
Rep: Reputation: Disabled
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...
 
Old 06-06-2012, 02:11 PM   #4
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 609

Rep: Reputation: 180Reputation: 180
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.
 
Old 06-06-2012, 02:15 PM   #5
Thodoris21
LQ Newbie
 
Registered: May 2012
Posts: 27

Original Poster
Rep: Reputation: Disabled
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?
 
Old 06-06-2012, 03:02 PM   #6
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 609

Rep: Reputation: 180Reputation: 180
I think you can copy its value into a global variable of the same type before threads creation...
 
Old 06-06-2012, 03:03 PM   #7
Thodoris21
LQ Newbie
 
Registered: May 2012
Posts: 27

Original Poster
Rep: Reputation: Disabled
Do you give an example of this?
 
Old 06-06-2012, 03:17 PM   #8
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 609

Rep: Reputation: 180Reputation: 180
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.
 
Old 06-06-2012, 03:19 PM   #9
Thodoris21
LQ Newbie
 
Registered: May 2012
Posts: 27

Original Poster
Rep: Reputation: Disabled
Does it work fine?
 
Old 06-06-2012, 03:22 PM   #10
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 609

Rep: Reputation: 180Reputation: 180
Try it with some of your variables.
 
Old 06-06-2012, 05:18 PM   #11
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
Quote:
Originally Posted by Thodoris21 View Post
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.
 
1 members found this post helpful.
Old 06-07-2012, 01:18 AM   #12
Thodoris21
LQ Newbie
 
Registered: May 2012
Posts: 27

Original Poster
Rep: Reputation: Disabled
I must do the exercise to pass the exams!
 
Old 06-07-2012, 01:33 AM   #13
Thodoris21
LQ Newbie
 
Registered: May 2012
Posts: 27

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by 414N View Post
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!
 
Old 06-07-2012, 01:49 AM   #14
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,731

Rep: Reputation: 1262Reputation: 1262Reputation: 1262Reputation: 1262Reputation: 1262Reputation: 1262Reputation: 1262Reputation: 1262Reputation: 1262
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....
 
Old 06-07-2012, 01:54 AM   #15
Thodoris21
LQ Newbie
 
Registered: May 2012
Posts: 27

Original Poster
Rep: Reputation: Disabled
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;
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] [scripting] try to passing arguments to for cycle (inside a function) vomplete Linux - General 3 04-26-2011 07:01 PM
[SOLVED] Passing array to thread function zak100 Programming 4 08-12-2010 11:07 AM
Problem with passing arguments in Perl bahadur Programming 1 05-30-2005 01:47 AM
C++, indefinite function arguments and multiple constructor passing @_@ R00ts Programming 2 04-08-2005 03:33 PM
Passing Arguments into the Thread Function George_gk Programming 2 01-31-2005 05:03 AM


All times are GMT -5. The time now is 05:16 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration