Chinaunix首页 | 论坛 | 博客
  • 博客访问: 473450
  • 博文数量: 120
  • 博客积分: 1853
  • 博客等级: 上尉
  • 技术积分: 1177
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-22 22:40
文章分类

全部博文(120)

文章存档

2013年(16)

2012年(104)

分类: LINUX

2012-05-26 10:47:15


点击(此处)折叠或打开

  1. #include <unistd.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <pthread.h>

  5. void *start(void *arg);
  6. int main(){
  7.     
  8.     pthread_t tpid;
  9.     int i = 1;
  10.     void *ret;
  11.     char buf[128];
  12.     
  13.     
  14.     getcwd(buf, 128);
  15.     printf("the current workdirectory is %s\n", buf);

  16.     if(pthread_create(&tpid, NULL, start, (void *)&i) < 0){//此处使用了地址,若改为(void *)i,则最后的结果显示i还是1,没有改变
  17.         perror(NULL);
  18.         exit(1);
  19.     }
  20.     pthread_join(tpid, &ret);
  21.     
  22.     printf("the exit code is %d\n",(int)ret);

  23.     printf("after pthread the i is %d\n", i);
  24.     
  25.     getcwd(buf, 128);
  26.     printf("the current workdirectory is %s\n", buf);
  27.     
  28.     return 0;
  29. }
  30. void *start(void *arg){
  31.     
  32.     printf("arg = %d\n",*(int *)arg);
  33.     *(int*)arg = *(int*)arg + 1;
  34.     chdir("/home/yyp/Desktop");//此处改变目录,主线程的工作目录也会改变
  35.     pthread_exit(*(int *)arg);
  36. }
进程的所有信息对进程所有的线程都是共享的,包括可执行的程序文本、程序的全局内存和堆内存、栈、以及文件描述符。

同时线程还包含,线程ID、一组寄存器值、栈、调度优先级和策略、信号屏蔽字(但是处理函数是共用的)、errno变量和私有数据。
阅读(1035) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~