Created
October 16, 2017 20:04
-
-
Save anonymous/90ddd689cc3e1d0ca92d2592fa0f9ba5 to your computer and use it in GitHub Desktop.
This file contains 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 <stdio.h> | |
#include <stdlib.h> | |
#include <pthread.h> | |
void thread(void) | |
{ | |
printf("This is a pthread.\n"); | |
} | |
int main(void) | |
{ | |
pthread_t id; | |
int i,ret; | |
ret=pthread_create(&id,NULL,(void *) thread,NULL); | |
if(ret!=0){ | |
printf ("Create pthread error!\n"); | |
exit (1); | |
} | |
printf("This is the main process.\n"); | |
return (0); | |
} |
This file contains 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 <stdio.h> | |
#include <stdlib.h> | |
#include <pthread.h> | |
int sharedi = 0; | |
void increse_num(void); | |
int main(){ | |
int ret; | |
pthread_t thrd1, thrd2, thrd3; | |
ret = pthread_create(&thrd1, NULL, (void *)increse_num, NULL); | |
ret = pthread_create(&thrd2, NULL, (void *)increse_num, NULL); | |
ret = pthread_create(&thrd3, NULL, (void *)increse_num, NULL); | |
pthread_join(thrd1, NULL); | |
pthread_join(thrd2, NULL); | |
pthread_join(thrd3, NULL); | |
printf("sharedi = %d\n", sharedi); | |
return 0; | |
} | |
void increse_num(void) { | |
long i,tmp; | |
for(i=1; i<=1000000; i++) { | |
tmp = sharedi; | |
tmp = tmp + 1; | |
sharedi = tmp; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment