C-pthread1

#include "stdio.h"
#include "pthread.h"

long int test( long int data);

int main()
 {
    pthread_t th1,th2;
    long int a=100;
    long int aa=100;
    int re=pthread_create(&th1,NULL,&test,a);
    int re1=pthread_create(&th2,NULL,&test,aa);
    if(re && re1)
     {
        printf("create thread failed\n");
        exit(1);
     }

    long int b,bb;
    pthread_join(th1,&b);
    pthread_join(th2,&bb);
    printf("the result is %ld\n",b);
    printf("the result is %ld\n",bb);
 }
long int test(long int  data)
 {
   long int i=0;
   long int sum=0;
   long int tmp=data;
   for(i=0;i<tmp*tmp;i++)
     sum+=i;
   return sum;
 }
                  

 

编译:gcc  -o pthread1 -l pthread pthread1.c

你可能感兴趣的:(c,pthread)