Chinaunix首页 | 论坛 | 博客
  • 博客访问: 359135
  • 博文数量: 79
  • 博客积分: 1270
  • 博客等级: 中尉
  • 技术积分: 1370
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-12 08:48
个人简介

freedom~~~~~~~~~~

文章分类

全部博文(79)

文章存档

2014年(10)

2013年(2)

2012年(13)

2011年(54)

分类: LINUX

2011-06-30 09:21:29

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

  4. int g_flag=0;

  5. void *thread1()
  6. {
  7.     printf("enter thread1\n");
  8.     printf("this is thread1,g_flag=%d,thread id is %u\n",g_flag,(unsigned int)pthread_self());
  9.     g_flag=1;
  10.     printf("this is thread1,g_flag=%d,thread id is %u\n",g_flag,(unsigned int)pthread_self());
  11.     printf("leave thread1");
  12.     pthread_exit(0);
  13. }

  14. void *thread2()
  15. {
  16.     printf("enter thread2");
  17.     printf("this is thread2,g_flag=%d,thread id is %u\n",g_flag,(unsigned int)pthread_self());
  18.     g_flag=2;
  19.     printf("this is thread2,g_flag=%d,thread id is %u\n",g_flag,(unsigned int)pthread_self());
  20.     printf("leave thread2");
  21.     pthread_exit(0);
  22. }

  23. int main()
  24. {
  25.     //int g_flag=0;
  26.     pthread_t tid1,tid2;
  27.     int rc1=pthread_create(&tid1,NULL,thread1,NULL);
  28.     if(rc1!=0)
  29.     {
  30.         printf("can't create rc1\n");
  31.         return(-1);
  32.     }

  33.     int rc2=pthread_create(&tid2,NULL,thread2,NULL);
  34.     if(rc2!=0)
  35.     {
  36.         printf("can't create rc2\n");
  37.         return(-1);
  38.     }

  39.     sleep(5);
  40. }
阅读(551) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~