initial import
[staff/goc4/2018bti7061.git] / c-7-compilation / examples / autotools / exercise3threads.c
1 #include <stdio.h>
2 #include <pthread.h>
3 #include "myLib.h"
4
5 #define MAX_LOOP 1000000000
6 pthread_t tid[2];
7
8 double expo;
9 long facto;
10
11 void* starterExponential(void* args){
12 long i = (long)args;
13
14 expo = exponential(i);
15 long j;
16 for(j=0;j<MAX_LOOP;j++){
17 if(j%(MAX_LOOP/10)==0){
18 printf("Exponential (loop %ld)\n",j);
19 }
20 }
21 }
22
23 void* starterFactorial(void* args){
24 long i= (long)args;
25 facto = factorial(i);
26 long j;
27 for(j=0;j<MAX_LOOP;j++){
28 if(j%(MAX_LOOP/10)==0){
29 printf("factorial (loop %ld)\n",j);
30 }
31 }
32 }
33
34 int main(int argc, char* argv[]){
35
36 long number;
37
38 printf("Enter a number\n");
39 int res = scanf(" %ld",&number);
40 if(res !=1) goto error;
41 pthread_create(&tid[0], NULL, starterExponential,(void*)number);
42
43 pthread_create(&tid[1], NULL, starterFactorial, (void*)number);
44 pthread_join(tid[0],NULL);
45 pthread_join(tid[1],NULL);
46 printf("exponential(%ld)=%lf and %ld! =%ld \n",number,expo,number,facto);
47
48 return 0;
49 error:
50 printf("The number is not valid\n");
51 return 1;
52 }