linux 线程总结
来源:互联网 发布:开源交换机网管软件 编辑:程序博客网 时间:2024/06/04 08:46
1、同一进程线程的共享资源
4、参数传递和资源共享的区别
1)参数传递:main函数中的一个结构体传入新建的线程中,代码如下:
#include<stdio.h>
#include<pthread.h>
typedef struct profile
{
char *name;
int high;
}S_Profile;
void *create(void *arg)
{
S_Profile *info = (S_Profile *)arg;
printf("the thread start ....\n");
printf("the name=%s and high=%d\n", info->name, info->high);
}
int main(int argc, char *argv[])
{
pthread_t tid;
S_Profile *pt;
int err;
pt = (S_Profile *)malloc(sizeof(S_Profile));
pt->name = "cxy";
pt->high = 173;
err = pthread_create(&tid, NULL, create, (void *)pt);
if(err != 0 )
{
printf("the thread create failed!\n");
return -1;
}
sleep(1);
printf("main end...\n");
}
输出结果:
the thread start ....
the name=cxy and high=173
main end...
2)资源共享 :新建立的线程可以共享进程中的数据
#include<stdio.h>
#include<pthread.h>
int a = 3;
void * create(void *argc)
{
printf("the thread start ....\n");
printf("the a=%d\n", a);
}
int main(int argc, char *argv[])
{
pthread_t tid;
int err;
int b = 2;
err = pthread_create(&tid, NULL, create, NULL);
if(err != 0)
{
printf("create sub thread failed!\n");
return -1;
}
sleep(1);
printf("the b=%d\n", b);
printf("main end...\n");
}
输出结果:
the thread start ....
the a=3
the b=2
main end...
- linux 线程总结
- ;linux 线程总结
- linux 线程总结
- Linux下线程总结
- 关于linux线程总结
- Linux线程属性总结
- Linux线程属性总结
- Linux线程学习总结
- linux下线程总结
- linux 线程总结
- Linux线程属性总结
- Linux线程属性总结
- Linux线程属性总结
- Linux线程属性总结
- linux 线程总结
- Linux线程属性总结
- Linux线程机制总结
- linux 线程边学边总结
- Window 通过cmd查看端口占用、相应进程、杀死进程等的命令
- 【9秒实验室自研】FLA文件资源导出工具 源码开放
- SOJ 1350. Piggy banks
- object-c runtime 的具体应用
- ios全局变量与使用
- linux 线程总结
- fatal: CRLF would be replaced by LF
- 软件类岗位都做些什么
- WORDPRESS本地环境搭建
- 重写与重载的重点
- 基于Firefly的卡牌手游源码《暗黑世界V1.4》完整源码下载!
- android基础知识:intent和intentfilter
- Firefly团队再出重拳,开源firefly+AS3的45度回合制rpg页游《烽烟OL》V1.0
- Net 如保证ListView增加一项后滚动条保证在最底部