LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-18-2013, 09:54 AM   #1
jyunker
Member
 
Registered: Aug 2009
Posts: 167

Rep: Reputation: 0
compiling a pthread program


am I am trying to compile a program whose code is as shown:

Code:
#include<pthread.h>

int sum1;
int sum2;

void thread1(int v[], int v_count) {
sum1 = 0;
int i;
for (i = 0; i < v_count; i++)
sum1 += v[i];
}
void thread2(int v[], int v_count) {
sum2 = 0;
int i;
for (i = 0; i < v_count; i++)
sum2 += v[i];
}


int main ( int argc, char** argv )
{
   pthread_t t1, t2;
   int N = 5000;
   int array1[N];
   
   pthread_create(&t1,NULL,thread1,array1,N);
   pthread_create(&t2,NULL,thread2,array1,N);
   pthread_join(t1,NULL);
     
}

The output is now shown:


Code:
gcc example2.c -o exam -lpthread -lm -lrt -g
example2.c: In function ‘main’:
example2.c:27: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type
/usr/include/pthread.h:225: note: expected ‘void * (*)(void *)’ but argument is of type ‘void (*)(int *, int)’
example2.c:27: error: too many arguments to function ‘pthread_create’
example2.c:28: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type
/usr/include/pthread.h:225: note: expected ‘void * (*)(void *)’ but argument is of type ‘void (*)(int *, int)’
example2.c:28: error: too many arguments to function ‘pthread_create’

I am trying to get two threads (thread1 and thread2) to run in parallell and thus create a problem of false sharing. I somehow cannot even get the program to compile.

I am sending each separate thread the same arguments. The array, array1, and the value of N. I think that this is the correct format. What am I doing wrong?

Any help appreciated. Thanks in advance.

Respectfully,


jyunker
 
Old 11-18-2013, 12:31 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 5,458
Blog Entries: 12

Rep: Reputation: 1905Reputation: 1905Reputation: 1905Reputation: 1905Reputation: 1905Reputation: 1905Reputation: 1905Reputation: 1905Reputation: 1905Reputation: 1905Reputation: 1905
Check out some of the examples here Pthread Examples.

You have to pass a void pointer, as the compiler is telling you. I don't think you can pass more than that one argument, you'd have to create a structure and cast it to the void pointer.
 
Old 11-18-2013, 02:26 PM   #3
jyunker
Member
 
Registered: Aug 2009
Posts: 167

Original Poster
Rep: Reputation: 0
compiling a pthreads program

Okay here is my new code, I created a struct name arg_struct with my two arguments that and I will pass struct
to thread1 and thread2:

Code:
#include<pthread.h>

int sum1;
int sum2;

struct arg_struct  {
   int arg1;
   int arg2;
}; 


void thread1(int v[], int v_count) {
    
     struct arg_struct *args = (struct arg_struct *)args;
     sum1 = 0;
     int i;
     for (i = 0; i < v_count; i++) {
//   printf ( "%d ", i );
     sum1 += v[i];
}
}
void thread2(int v[], int v_count) {

     struct arg_struct *args = (struct arg_struct *)args;
     sum2 = 0; 
     int i;
     for (i = 0; i < v_count; i++) {
//   printf ( "%d ", i );
     sum2 += v[i];
}
}


int main ( int argc, char** argv )
{
   pthread_t t1, t2;
   int N = 5000;
   int array1[N];
   struct arg_struct args;
   args.arg1 = array1;
   args.arg2 = N;

   
   pthread_create(&t1,NULL,&thread1,(void *)&args));
   pthread_create(&t2,NULL,&thread2,(void *)&args));
   pthread_join(t1,NULL);
     
}








and when I run it, I get the following errors:

gcc example2.c -o exam -lpthread -lm -lrt -g
example2.c: In function ‘main’:
example2.c:41: warning: assignment makes integer from pointer without a cast
example2.c:45: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type
/usr/include/pthread.h:225: note: expected ‘void * (*)(void *)’ but argument is of type ‘void (*)(int *, int)’
example2.c:45: error: expected ‘;’ before ‘)’ token
example2.c:45: error: expected statement before ‘)’ token
example2.c:46: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type
/usr/include/pthread.h:225: note: expected ‘void * (*)(void *)’ but argument is of type ‘void (*)(int *, int)’
example2.c:46: error: expected ‘;’ before ‘)’ token
example2.c:46: error: expected statement before ‘)’ token


Now what am I doing wrong? This is my first time programming with pthreads and I am making too
many errors.

Any help appreciated. Thanks in advance.

Respectfully,

jyunker
 
Old 11-18-2013, 02:38 PM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 5,458
Blog Entries: 12

Rep: Reputation: 1905Reputation: 1905Reputation: 1905Reputation: 1905Reputation: 1905Reputation: 1905Reputation: 1905Reputation: 1905Reputation: 1905Reputation: 1905Reputation: 1905
You have fundamental syntax errors in there which have nothing to do with pthreads, they're just general errors in composing a C program. Read the output of the compiler errors and warnings, go to the lines it is complaining about, and fix your syntax errors.

I suggest you should review the p_greetings.c or p_cpi.c examples in the link previously sent.
 
  


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] Very simple pthread program sometimes prints garbage value for a variable gregorian Programming 7 05-16-2012 11:30 AM
[SOLVED] pthread not found when compiling vbox for Slackware FreakyPoint Linux - Virtualization and Cloud 1 03-30-2012 03:51 AM
[SOLVED] multithreaded program,pthread ankit,garg Programming 6 01-19-2012 05:27 AM
gdb crashes when debugging pthread-ed program tbruer1138 Programming 0 04-21-2005 05:38 PM
Error compiling with pthread Siriuss Programming 1 10-23-2004 08:21 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:24 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration