#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h> void *thread_foo_func(void *);
void *thread_bar_func(void *); int global = ; int main(){
int local = ;
int foo, bar;
pthread_t fthread, bthread;
foo = pthread_create(&fthread, NULL, thread_foo_func, (void *)&local);
bar = pthread_create(&bthread, NULL, thread_bar_func, (void *)&local);
if (foo != || bar != ){
printf("thread creation failed.\n");
return -;
} foo = pthread_join(fthread, NULL);
bar = pthread_join(bthread, NULL);
if (foo != || bar != ){
printf("thread join failed.\n");
return -;
} printf("In thread main");
printf("address of global %d: %x\n", global, &global);
printf("address of main local %d: %x\n", local, &local); return ;
} void *thread_foo_func(void *arg){
int foo_local = ;
global ++;
*(int *)arg = ;
printf("In thread foo_func");
printf("address of global %d: %x\n", global, &global);
printf("address of main local %d: %x\n", *(int *)arg, arg);
printf("address of foo local: %x\n", &foo_local);
printf("\n");
} void *thread_bar_func(void *arg){
int bar_local = ;
global ++;
*(int *)arg = ;
printf("In thread bar_func");
printf("address of global %d: %x\n", global, &global);
printf("address of main local %d: %x\n", *(int *)arg, arg);
printf("address of bar local: %x\n", &bar_local);
printf("\n");
}

打印输出结果

In thread foo_funcaddress of global : 8049a48
address of main local : bfc567b8
address of foo local: b7f553c4 In thread bar_funcaddress of global : 8049a48
address of main local : bfc567b8
address of bar local: b75543c4 In thread mainaddress of global : 8049a48
address of main local : bfc567b8

可见:

1 global 在线程中可见,而且共享,

2 main中的loca通过指针传给了进程,进程可以改变

3 两个线程中的私有变量是不同的,是线程私有的。

这和fork出的进程就完全不同

 int global = ;

 int main(int argc,char** argv)
{
int var=;
int pid=fork();
if(pid==-){
printf("error!");
}
else if(pid==){
global++;
var++;
printf("This is the child process!\n");
}
else{
printf("This is the parent process! child processid=%d\n",pid);
}
printf("%d, %d, %d \n", getpid(), global, var); return ;
}

打印结果:

This is the child process!
, ,
This is the parent process! child processid=
, ,

可见,调用fork,会有两次返回,一次是父进程、一次是子进程,因为子进程是父进程的副本,所以它拥有父进程数据空间、栈和堆的副本,它们并没有共享这些存储空间,它们只共享正文段。

pthread 学习系列 case1-- 共享进程数据 VS 进程的更多相关文章

  1. pthread 学习系列 case2-- 使用互斥锁

    ref http://www.ibm.com/developerworks/cn/linux/thread/posix_thread1/index.html #include <pthread. ...

  2. Kettle学习系列之数据仓库、数据整合、ETL、ELT和EII之间的区别?

    不多说,直接上干货! 在数据仓库领域里,的一个重要概念就是数据整合(data intergration).数据整合它就是把不同数据库中的数据整合到一起,对外提供统一的数据视图. 数据整合最典型的案例就 ...

  3. pthread 学习系列 case2-- pthread_mutex_t

    许多互斥对象 如果放置了过多的互斥对象,代码就没有什么并发性可言,运行起来也比单线程解决方案慢.如果放置了过少的互斥对象,代码将出现奇怪和令人尴尬的错误.幸运的是,有一个中间立场.首先,互斥对象是用于 ...

  4. 【深度学习系列】PaddlePaddle之数据预处理

    上篇文章讲了卷积神经网络的基本知识,本来这篇文章准备继续深入讲CNN的相关知识和手写CNN,但是有很多同学跟我发邮件或私信问我关于PaddlePaddle如何读取数据.做数据预处理相关的内容.网上看的 ...

  5. Caffe学习系列(14):初识数据可视化

    //   首先将caffe的根目录作为当前目录,然后加载caffe程序自带的小猫图片,并显示. 图片大小为360x480,三通道 In [1]: import numpy as np import m ...

  6. Echarts 学习系列(3)-Echarts动态数据交互

    写在前面 上一小节,我们总结了折线(面积)图.柱状(条形)图.饼(圆环)图类型的图表. 但是,都是静态的.接下来的,这一小节,总结的是Echarts 动态数据的交换. 前置条件 开发环境:win10 ...

  7. 【深度学习系列】关于PaddlePaddle的一些避“坑”技巧

    最近除了工作以外,业余在参加Paddle的AI比赛,在用Paddle训练的过程中遇到了一些问题,并找到了解决方法,跟大家分享一下: PaddlePaddle的Anaconda的兼容问题 之前我是在服务 ...

  8. 【深度学习系列】PaddlePaddle垃圾邮件处理实战(二)

    PaddlePaddle垃圾邮件处理实战(二) 前文回顾   在上篇文章中我们讲了如何用支持向量机对垃圾邮件进行分类,auc为73.3%,本篇讲继续讲如何用PaddlePaddle实现邮件分类,将深度 ...

  9. Caffe 学习系列

    学习列表: Google protocol buffer在windows下的编译 caffe windows 学习第一步:编译和安装(vs2012+win 64) caffe windows学习:第一 ...

随机推荐

  1. ZooKeeper学习第八期——ZooKeeper伸缩性

    一.ZooKeeper中Observer 1.1 ZooKeeper角色 经过前面的介绍,我想大家都已经知道了在ZooKeeper集群当中有两种角色Leader和Follower.Leader可以接受 ...

  2. MATLAB中提高fwrite和fprintf函数的I/O性能

    提高fwrite和fprintf函数的I/O性能 http://www.matlabsky.com/thread-34861-1-1.html     今天我们将讨论下著名的fwrite(fprint ...

  3. ios —— UIViewAdditions 布局坐标类库

    方便大家计算视图的高度,宽度,上下左右坐标,简化代码操作,更加直观 下载地址:http://download.csdn.net/detail/humingtao2013/7511657

  4. iOS开发工具(上班需要必备的软件)

    1 源代码管理工具 SVN:SVN可以使用的客户端软件有Cornerstone,SmartSVN,svnX,乌龟SVN,莲花版svn等等 或者git(sourcetree) 2 有道词典 3 Foxm ...

  5. Opencv step by step - 鼠标事件

    鼠标事件有下面几种(没有滚轮事件,比较遗憾): #define CV_EVENT_MOUSEMOVE 0 滑动 #define CV_EVENT_LBUTTONDOWN 1 左键点击 #define ...

  6. ajax请求模拟登录

    前台 @if (Session["username"] != null) { <div class="login"> <span style= ...

  7. form表单用ge方式提交时ie显示中文参数乱码

    有网友说 通过给form表单添加accept-charset="gb2312"和 onsubmit="document.charset='gb2312'" 但这 ...

  8. Coding the Matrix (1):向量

    1. list 画点 >>> from plotting import plot >>> L = [[2, 2], [3, 2], [1.75, 1], [2, 1 ...

  9. 第十七课:js数据缓存系统的原理

    这一章主要讲的是jQuery的缓存系统的历史发展,以及他自己的框架的缓存系统的实现.都是源码解析. 我就挑几个重点讲下: (1)jQuery的缓存机制的原理 jQuery的缓存机制实现的原理是在元素中 ...

  10. 第十四章 校本化CSS

    CSS(层叠样式表)是一种指定HTML文档视觉的表现的标准.CSS本来是让视觉设计师来使用的:它允许设计师精确的对文档元素的字体 ,颜色,外边距,缩进,边框甚至是定位.不过,客户端javascript ...