simple multithreading error in C? -


i new c language. have written piece of code creates 2 thread calculate 2 different results. code works compiling showing errors , want know error. can me? new c, guess might silly mistake...

errors:

q3.c: in function ‘main’: q3.c:63:2: warning: passing argument 3 of ‘pthread_create’ incompatible pointer type [enabled default] in file included q3.c:5:0: /usr/include/pthread.h:225:12: note: expected ‘void * (*)(void *)’ argument of type ‘double * (*)(void *)’ q3.c:64:9: warning: passing argument 3 of ‘pthread_create’ incompatible pointer type [enabled default] in file included q3.c:5:0: /usr/include/pthread.h:225:12: note: expected ‘void * (*)(void *)’ argument of type ‘double * (*)(void *)’ 

the code:

#include <stdlib.h> #include <pthread.h> #include <string.h> #include<stdio.h> #include<math.h> #define pi 3.1415  int i, x, n;  double *sin_x(void* dimension); double *cos_x(void* dimension); int fact(int); struct data {   int ang_deg;   int no_of_terms; };  int fact(int num) {   int f = 0;    if (num == 1)     return 1;   else     f = num * fact(num - 1);    return f; }  int main(int argc, char argv[]) {   printf("\nenter x , n:\t");   scanf("%d %d", &x, &n);    struct data   {     int ang_rad;     int no_of_terms;   };    struct data dimension;  // automatic allocation, fields placed on stack    dimension.ang_rad = x;   dimension.no_of_terms = n;    pthread_t thrd1, thrd2;   int thret1, thret2;    thret1 = pthread_create(&thrd1, null, sin_x, (void *) &dimension);   thret2 = pthread_create(&thrd2, null, cos_x, (void *) &dimension);    pthread_join(thrd1, null );   pthread_join(thrd2, null );    //printf("\nthret1 = %d\n", thret1);   //printf("thret2 = %d\n", thret2);   sleep(5);   printf("parent thread exiting...\n");    exit(1);    return 0; }  double *sin_x(void* dimension) {   struct data* dim = (struct data*) dimension;   int ang_deg = dim->ang_deg;   int no_of_terms = dim->no_of_terms;    //int ang_deg, int no_of_terms   int term, j;   double value = 0.0, ang_rad = 0.0;   ang_rad = (double) ang_deg * pi / 180;    (term = 1, j = 2; term < no_of_terms * 2; term += 2, j++)   {     value += (double) pow(-1.0, j) * pow(ang_rad, term) / fact(term);   }    printf("\nsin(%d) = %f", ang_deg, value);   double *a = &value;    return a; }  double *cos_x(void* dimension) {   struct data* dim = (struct data*) dimension;   int ang_deg = dim->ang_deg;   int no_of_terms = dim->no_of_terms;    int term, j;   double value = 1.0, ang_rad = 0.0;   ang_rad = (double) ang_deg * pi / 180;    (term = 2, j = 1; term <= no_of_terms; term += 2, j++)   {     value += (double) pow(-1.0, j) * pow(ang_rad, term) / fact(term);   }    printf("\ncos(%d) = %f", ang_deg, value);   double *a = &value;    return a; } 

changes:

  • replaced sleep() pthread_exit(). sleep poor idea synchronization.
  • corrected main()'s parameters. should int main(void) or int main(int argc, char**argv) or equivalent.
  • removed unnecessary struct inside main().
  • changed return type of fact() size_t, allow calculate more factorials possible int type.
  • rewrote factorial function use memoization. more efficient way of calculating factorial since don't need recalculate previous factorials have been computed already. array size 64k can change. stands, can calculate first 64k factorials.
  • you should add eror checking pthread_ functions.
  • removed pthread_join() calls since main() thread exits. process exit naturally once last thread exits.
  • removed thread values since not using them. if want know computed values, can add member struct dimension , store result in that. method allow return complex value thread functions.

--

here's functional code above changes done. more info, read pthread_create() , manuals of other functions.

#include <stdlib.h> #include <pthread.h> #include <string.h> #include<stdio.h> #include<math.h> #define pi 3.1415  int i, x, n; void *sin_x( void* dimension ); void *cos_x( void* dimension ); size_t fact( int );  size_t tab[64*1024] = {1}; struct data             {                 int ang_deg;                 int no_of_terms;             };    size_t fact( int num ) {       size_t f = 0;        if ( tab[num]) return tab[num];        f = num * fact( num - 1 );       tab[num] = f;        return f; }  int main (int argc, char **argv) {          printf( "\nenter x , n:\t" );         scanf( "%d %d", &x, &n );          struct data dimension;  // automatic allocation, fields placed on stack          dimension.ang_deg = x;         dimension.no_of_terms= n;          pthread_t thrd1, thrd2;         int thret1, thret2;           thret1 = pthread_create(&thrd1, null, sin_x, &dimension);         thret2 = pthread_create(&thrd2, null, cos_x, &dimension);             printf("parent thread exiting...\n");         pthread_exit(0); }  void *sin_x( void* dimension ) {     struct data* dim = dimension;     int ang_deg = dim->ang_deg;     int no_of_terms = dim->no_of_terms;     int term, j;     double value = 0.0, ang_rad = 0.0;     ang_rad = ( double ) ang_deg * pi / 180;      ( term = 1, j = 2;term < no_of_terms*2;term += 2, j++ )         {             value += ( double ) pow( -1.0, j ) * pow( ang_rad, term ) / fact( term );         }     printf("\nsin(%d) = %f", ang_deg, value);     return 0; }  void *cos_x( void* dimension ) {     struct data* dim = dimension;     int ang_deg = dim->ang_deg;     int no_of_terms = dim->no_of_terms;     int term, j;     double value = 1.0, ang_rad = 0.0;     ang_rad = ( double ) ang_deg * pi / 180;      ( term = 2, j = 1;term <= no_of_terms;term += 2, j++ )         {             value += ( double ) pow( -1.0, j ) * pow( ang_rad, term ) / fact( term );         }      printf("\ncos(%d) = %f", ang_deg, value);     double *a = &value;     return 0; } 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -