Chinaunix首页 | 论坛 | 博客
  • 博客访问: 590591
  • 博文数量: 150
  • 博客积分: 1132
  • 博客等级: 少尉
  • 技术积分: 2067
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-11 16:46
文章分类

全部博文(150)

文章存档

2015年(15)

2014年(75)

2013年(4)

2012年(56)

分类: LINUX

2014-11-24 23:07:42


  1. #include <stdio.h>
  2. #include <pthread.h>

  3. int *pa = NULL;

  4. void * fn_thread1(void * c);

  5. int main(void)
  6. {
  7.     pthread_t tid;
  8.     int err = 0;

  9.     int a = 4;
  10.     pa = &a;

  11.     if ((err = pthread_create(&tid, NULL, fn_thread1, NULL)) != 0) {
  12.         fprintf(stderr, "pthread_create error:%d\n", err);
  13.         return -1;
  14.     }

  15.     sleep(1);
  16.     return 0;
  17. }

  18. void * fn_thread1(void * c)
  19. {
  20.     printf("thread1: a = %d\n", *pa);
  21. }
运行结果:
thread1: a = 4

只有寄存器中的内容线程间互相访问不到,其他凡是进程地址空间的都能访问到,包括线程栈中的内容。
阅读(1239) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~