Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pthread.h>
- #include <stdio.h>
- #include <stdlib.h>
- #define NUMTHREADS 4
- #define N 3
- double *a, *b;
- void *dot(void *arg)
- {
- int start, end ;
- long offset, i;
- double *mysum = malloc(sizeof(double));
- *mysum=0.0;
- offset = (long)arg;
- start = offset*N;
- end = start + N;
- for (i=start; i<end ; i++)
- {
- *mysum += (a[i] * b[i]);
- }
- printf("ja wyliczylem %d, %d, %f \n", start, end, *mysum);
- pthread_exit((void *) mysum);
- }
- int main (int argc, char *argv[])
- {
- pthread_t watki[NUMTHREADS];
- long i;
- double iloczyn=0.0;
- double *loc_iloczyn;
- pthread_attr_t atr;
- a= (double*) malloc (NUMTHREADS*N*sizeof(double));
- b= (double*) malloc (NUMTHREADS*N*sizeof(double));
- for (i=0; i<N*NUMTHREADS; i++) {
- a[i]=1.0;
- b[i]=2.0*a[i];
- }
- pthread_attr_init (&atr);
- pthread_attr_setdetachstate( &atr, PTHREAD_CREATE_JOINABLE);
- for(i=0; i<NUMTHREADS; i++)
- {
- pthread_create(&(watki[i]), NULL, dot, (void *)i);
- }
- for(i=0; i<NUMTHREADS; i++) {
- pthread_join(watki[i], (void **)&loc_iloczyn);
- iloczyn = iloczyn + *loc_iloczyn;
- }
- pthread_attr_destroy(&atr);
- printf ("iloczyn = %f \n", iloczyn);
- free(a);
- free(b);
- free(loc_iloczyn);
- pthread_exit(NULL);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement