Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pthread.h>
- #include <stdio.h>
- #include <stdlib.h>
- int a = 10;
- void *mythread1(void *dummy)
- {
- pthread_t mythid;
- mythid = pthread_self();
- a += 10;
- printf("Thread %lu, result = %d\n", mythid, a);
- return NULL;
- }
- void *mythread2(void *dummy)
- {
- pthread_t mythid;
- mythid = pthread_self();
- a *= 3;
- printf("Thread %lu, result = %d\n", mythid, a);
- return NULL;
- }
- void *mythread3(void *dummy)
- {
- pthread_t mythid;
- mythid = pthread_self();
- a %= 7;
- printf("Thread %lu, result = %d\n", mythid, a);
- return NULL;
- }
- int main()
- {
- pthread_t thid1, thid2, thid3;
- int result1 = pthread_create (&thid1, (pthread_attr_t *)NULL, mythread1, NULL);
- if(result1 != 0) { printf ("Error on thread create, return value = %d\n", result1); exit(-1); }
- printf("Thread has been created, thid = %lu\n", thid1);
- int result2 = pthread_create( &thid2, (pthread_attr_t *)NULL, mythread2, NULL);
- if(result2 != 0) { printf ("Error on thread create, return value = %d\n", result2); exit(-1); }
- printf("Thread has been created, thid = %lu\n", thid2);
- int result3 = pthread_create( &thid3, (pthread_attr_t *)NULL, mythread3, NULL);
- if(result3 != 0){ printf ("Error on thread create, return value = %d\n", result3); exit(-1); }
- printf("Thread has been created, thid = %lu\n", thid3);
- pthread_join(thid1, (void **)NULL);
- pthread_join(thid2, (void **)NULL);
- pthread_join(thid3, (void **)NULL);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment