Last active
September 29, 2016 13:53
-
-
Save kotaroito/2ed7cf12eb77f7d26f35cf92908ad415 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <pthread.h> | |
int x = 0; | |
int n = 100; | |
pthread_t tid[100]; | |
void* calc() | |
{ | |
int j = 0; | |
for (j = 0; j < 10000000; j++) { | |
x = x + 1; | |
x = x - 1; | |
} | |
return ((void *)0); | |
} | |
int main() | |
{ | |
int i = 0; | |
int err; | |
for (i = 0; i < n; i++) { | |
err = pthread_create(&(tid[i]), NULL, &calc, NULL); | |
if (err != 0) | |
printf("\ncan't create thread :[%s]", strerror(err)); | |
} | |
for (i = 0; i < n; i++) { | |
err = pthread_join(tid[i], NULL); | |
if (err != 0) | |
printf("\ncan't join with thread :[%s]", strerror(err)); | |
} | |
printf("x is %d\n", x); | |
exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment