Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <math.h>
- struct pairs
- {
- double val;
- double res;
- };
- void * calcThread(void * val)
- {
- struct pairs * pair = (struct pairs *)val;
- pair->res = sin(pair->val*M_PI/180);
- pthread_exit(0);
- }
- int main(int argc, char * argv[])
- {
- int i = 1;
- struct pairs * values = (struct pairs *)malloc((argc-1) * sizeof(struct pairs));
- pthread_t * threads = (pthread_t *)malloc((argc-1) * sizeof(pthread_t));
- void * tmp;
- for(i; i < argc; i++)
- {
- values[i-1].val = strtod(argv[i], NULL);
- pthread_create(&(threads[i-1]), NULL, &calcThread, (void *)&(values[i-1]));
- }
- for(i = 0; i < argc - 1; i++)
- {
- pthread_join(threads[i], tmp);
- printf("THREAD %lu: sin(%lf) = %lf\n", pthread_self(), values[i].val, values[i].res);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement