Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pthread.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #define NUM_THREADS 16
- /**********************************
- * compilare con *
- * cc pthread.c -pthread -lm *
- **********************************/
- void *operaio(void *t)
- {
- int i;
- long n;
- pthread_t ThId= pthread_self();
- double risultato=0.0;
- n = (long)t;
- printf("Thread numero %ld (con ThreadID %ld) partito...\n",n,ThId);
- for (i=0; i<1000000; i++)
- {
- risultato = risultato + sin(i) * tan(i);
- }
- printf("Thread %ld terminato. Risultato = %e\n",n, risultato);
- pthread_exit((void*) t);
- }
- int main (int argc, char *argv[])
- {
- pthread_t thread[NUM_THREADS];
- int ret;long t;
- void *status;
- /* Boss: inizializza i thread */
- for(t=0; t<NUM_THREADS; t++) {
- printf("Main: creating thread %ld\n", t);
- ret = pthread_create(&thread[t], NULL, operaio, (void *)t);
- if (ret) {printf("ERR: pthread_create() ha restituito %d\n", ret);
- exit(-1);}
- }
- /* Boss: aspetta il termine dei thread */
- for(t=0; t<NUM_THREADS; t++) {
- ret = pthread_join(thread[t], &status);
- if (ret) {printf("ERR: pthread_join() ha restituito %d\n", ret);
- exit(-1);}
- printf("main(): riunito thread %ld (status=%ld)\n",t,(long)status);
- }
- printf("main(): fine esecuzione.\n");
- pthread_exit(EXIT_SUCCESS);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement