Thread(简单使用)
/***
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\n"); } else { printf("create thread 1 success\n"); } if(ret_thrd2 != 0) { printf("create thread 2 failed\n"); } else { printf("create thread 2 success\n"); } tmp1 = pthread_join(thread1,&retval); printf("thread1 return value(retval) is %d\n",(int)retval); printf("thread1 return value(tmp) is %d\n",tmp1); if(tmp1 != 0) { printf("cannot join with thread1\n"); } printf("thread1 end\n"); tmp2 = pthread_join(thread2,&retval); printf("thread2 return value(retval) is %d\n",(int)retval); printf("thread2 return value(tmp) is %d\n",tmp2); if(tmp2 != 0) { printf("cannot join with thread2\n"); } printf("thread2 end\n"); return 0; } void print_message_function(void* ptr) { int i; for(i = 0; i < 5; i++) { printf("%s:%d\n",(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.\n"); } } int main() { pthread_t id; int i,ret; ret = pthread_create(&id,NULL,(void*)thread,NULL); if(ret != 0) { printf("Create pthread error!\n"); exit(1); } for(i = 0; i < 3; i++) { printf("This is the main process.\n"); } 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\n",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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
2018-07-25 1045 Favorite Color Stripe (30)