/***
thread.c
***/
#include<stdio.h> #include<stdlib.h> #include<pthread.h> void print_message_function(void* ptr); int main() { int tmp1,tmp2; void* retval; pthread_t thread1,thread2; char* message1 = "thread1"; char* message2 = "thread2"; int ret_thrd1,ret_thrd2; ret_thrd1 = pthread_create(&thread1,NULL,(void*)&print_message_function,(void*)message1); ret_thrd2 = pthread_create(&thread2,NULL,(void*)&print_message_function,(void*)message2); if(ret_thrd1 != 0) { printf("create thread 1 failed "); } else { printf("create thread 1 success "); } if(ret_thrd2 != 0) { printf("create thread 2 failed "); } else { printf("create thread 2 success "); } tmp1 = pthread_join(thread1,&retval); printf("thread1 return value(retval) is %d ",(int)retval); printf("thread1 return value(tmp) is %d ",tmp1); if(tmp1 != 0) { printf("cannot join with thread1 "); } printf("thread1 end "); tmp2 = pthread_join(thread2,&retval); printf("thread2 return value(retval) is %d ",(int)retval); printf("thread2 return value(tmp) is %d ",tmp2); if(tmp2 != 0) { printf("cannot join with thread2 "); } printf("thread2 end "); return 0; } void print_message_function(void* ptr) { int i; for(i = 0; i < 5; i++) { printf("%s:%d ",(char*)ptr,i); } }
运行结果:
exbot@ubuntu:~/wangqinghe/thread/thread_0530$ ./thread
create thread 1 success
create thread 2 success
thread2:0
thread2:1
thread2:2
thread2:3
thread2:4
thread1:0
thread1:1
thread1:2
thread1:3
thread1:4
thread1 return value(retval) is 10
thread1 return value(tmp) is 0
thread1 end
thread2 return value(retval) is 10
thread2 return value(tmp) is 0
thread2 end
/*** simple example ***/ #include<stdio.h> #include<pthread.h> void thread(void) { int i; for(i = 0; i < 3; i++) { printf("This is a pthread. "); } } int main() { pthread_t id; int i,ret; ret = pthread_create(&id,NULL,(void*)thread,NULL); if(ret != 0) { printf("Create pthread error! "); exit(1); } for(i = 0; i < 3; i++) { printf("This is the main process. "); } pthread_join(id,NULL); return 0; }
运行结果:
exbot@ubuntu:~/wangqinghe/thread/thread_0530$ ./thread1
This is the main process.
This is the main process.
This is the main process.
This is a pthread.
This is a pthread.
This is a pthread.
/*** pthread.c ***/ //gcc pthread.c -o pthread -lpthread #include<stdio.h> #include<stdlib.h> #include<string.h> #include<unistd.h> #include<pthread.h> void * print_a(void*); void * print_b(void*); int main() { pthread_t t0; pthread_t t1; if(pthread_create(&t0,NULL,print_a,NULL) == -1) { puts("fail to create pthread t0"); exit(1); } if(pthread_create(&t1,NULL,print_b,NULL) == -1) { puts("fail to create pthread t1"); exit(1); } void* result; if(pthread_join(t0,&result) == -1) { puts("fail to recollect t0"); exit(1); } if(pthread_join(t1,&result) == -1) { puts("fail to create recollect t1"); exit(1); } return 0; } void * print_a(void*) { for(int i = 0; i < 10 ; i++) { sleep(1); puts("aa"); } return NULL; } void * print_b(void*) { for(int i = 0; i < 20; i++) { sleep(1); puts("bb"); } return NULL; }
运行结果:
exbot@ubuntu:~/wangqinghe/thread$ ./pthread
bb
aa
bb
aa
bb
aa
bb
aa
aa
bb
bb
aa
bb
aa
aa
bb
bb
aa
aa
bb
bb
bb
bb
bb
bb
^C
/*** no_mutex.c ***/ #include<stdio.h> #include<stdlib.h> #include<pthread.h> int sharedi = 0; void increase_num(void); int main() { int ret; pthread_t thrd1,thrd2,thrd3; ret = pthread_create(&thrd1,NULL,(void *)increase_num,NULL); ret = pthread_create(&thrd2,NULL,(void*)increase_num,NULL); ret = pthread_create(&thrd3,NULL,(void*)increase_num,NULL); pthread_join(thrd1,NULL); pthread_join(thrd2,NULL); pthread_join(thrd3,NULL); printf("sharedi = %d ",sharedi); return 0; } void increase_num(void) { long i,tmp; for(i = 0 ; i < 1000; i++) { tmp = sharedi; tmp = tmp + 1; sharedi = tmp; } }
运行结果:
exbot@ubuntu:~/wangqinghe/thread/thread_0611$ ./no_mutex
sharedi = 3000