LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   compiling a pthread program (https://www.linuxquestions.org/questions/linux-newbie-8/compiling-a-pthread-program-4175485012/)

jyunker 11-18-2013 09:54 AM

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

rtmistler 11-18-2013 12:31 PM

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.

jyunker 11-18-2013 02:26 PM

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

rtmistler 11-18-2013 02:38 PM

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.


All times are GMT -5. The time now is 09:00 PM.