打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
linux下的C语言开发(多线程编程)
http://www.aiuxian.com/article/p-1286301.html
2014
多线程和多进程还是有很多区别的。其中之一就是,多进程是linux内核本身所支持的,而多线程则需要相应的动态库进行支持。对于进程而言,数据之间都是相互隔离的,而多线程则不同,不同的线程除了堆栈空间之外所有的数据都是共享的。说了这么多,我们还是自己编写一个多线程程序看看结果究竟是怎么样的。
[cpp]
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
void func_1(void* args)
{
while(1){
sleep(1);
printf("this is func_1!\n");
}
}
void func_2(void* args)
{
while(1){
sleep(2);
printf("this is func_2!\n");
}
}
int main()
{
pthread_t pid1, pid2;
if(pthread_create(&pid1, NULL, func_1, NULL))
{
return -1;
}
if(pthread_create(&pid2, NULL, func_2, NULL))
{
return -1;
}
while(1){
sleep(3);
}
return 0;
}
和我们以前编写的程序有所不同,多线程代码需要这样编译,输入gcc thread.c -o thread -lpthread,编译之后你就可以看到thread可执行文件,输入./thread即可。
[cpp]
[test@localhost Desktop]$ ./thread
this is func_1!
this is func_2!
this is func_1!
this is func_1!
this is func_2!
this is func_1!
this is func_1!
this is func_2!
this is func_1!
this is func_1!
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Linux 线程学习(一)
Linux多线程pthread
reentrant函数与thread safe函数浅析
linux下C语言多线程编程实例
Linux——线程编程
在linux c++类中的成员函数里创建多线程要注意的地方(转)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服