博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C 多线程学习
阅读量:4684 次
发布时间:2019-06-09

本文共 1286 字,大约阅读时间需要 4 分钟。

一直都觉得多线程啥的是个比较麻烦的东西,今天好好看了下,写了个DEMO出来。

看看怎么灵活运用,估计还得再多写写,回头改造下之前那个例子。

#include
#include
char* print_hello(int num);void create_result(int t_res);void main(){ int tmp1,tmp2; void *retval; pthread_t thread1,thread2; int res_thred1,res_thred2; //创建线程,判断是否成功 create_result(pthread_create(&thread1,NULL,(void *)&print_hello,(void *) 1)); create_result(pthread_create(&thread2,NULL,(void *)&print_hello,(void *) 2)); //join掉线程并返回结果 tmp1=pthread_join(thread1,&retval); printf("thread2 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(tmp) is %d\n",tmp2); if (tmp2 !=0){ printf("cannot join with thread1\n"); } printf("thread1 end\n");}char* print_hello(int num){ printf("hello,word %d\n",num);}void create_result(int t_res){ if(t_res !=0){ printf("创建失败\n"); }else{ printf("创建成功\n"); }}

编译的时候会有搓B问题,gg之。

undefined reference to 'pthread_create'undefined reference to 'pthread_join'

该问题原因:pthread 库不是 Linux 系统默认的库,连接时需要使用静态库 libpthread.a,所以在使用pthread_create()创建线程,以及调用 pthread_atfork()函数建立fork处理程序时,需要链接该库

解决方法:

gcc thread.c -o thread -lpthread

 

转载于:https://www.cnblogs.com/xiaoCon/p/3665603.html

你可能感兴趣的文章
CSS布局--垂直水平居中
查看>>
MFC中 用Static控件做超链接(可以实现变手形、下划线、字体变色等功能)
查看>>
20144303 《Java程序设计》第五周学习总结
查看>>
多线程(第三天)
查看>>
python 抓取小说网站,制作电子书。
查看>>
restframework视图三部曲
查看>>
失去光标display=none事件的坑
查看>>
Python3.x:函数定义
查看>>
NOI 2014 起床困难综合症
查看>>
[LeetCode] Majority Element II
查看>>
设计模式的理解
查看>>
[cocos2dx动作]CCLabel类数字变化动作
查看>>
(转)Excel的 OleDb 连接串的格式(连接Excel 2003-2013)
查看>>
JAVA面试——分布式锁
查看>>
HDU2588--GCD(欧拉函数)
查看>>
负载均衡服务器
查看>>
ruby之gem install
查看>>
Linux下samba编译与安装(Ubuntu和嵌入式linux)
查看>>
jquery 获取后台实时数据
查看>>
BZOJ 3239 Discrete Logging(BSGS)
查看>>