Skip to content

Instantly share code, notes, and snippets.

Created October 16, 2017 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/90ddd689cc3e1d0ca92d2592fa0f9ba5 to your computer and use it in GitHub Desktop.
Save anonymous/90ddd689cc3e1d0ca92d2592fa0f9ba5 to your computer and use it in GitHub Desktop.
#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);
}
#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