Chinaunix首页 | 论坛 | 博客
  • 博客访问: 631298
  • 博文数量: 1008
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 5175
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-31 09:44
文章分类
文章存档

2012年(1008)

我的朋友

分类:

2012-08-01 11:15:34

原文地址:线程属性设置 作者:luozhiyong131

 

  1. /*
  2.  * 设置线程属性——线程的分离状态
  3.  * Lzy 2011-6-18
  4.  */

  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <pthread.h>

  8. int thread_flag = 1;

  9. void *mythread(void *arg)        //定义新线程运行函数
  10. {
  11.     printf("Thread is running\n");
  12.     sleep(3);
  13.     printf("Thread is exiting now\n");
  14.     thread_flag = 0;
  15.     pthread_exit(NULL);
  16. }

  17. int main(void)
  18. {
  19.     pthread_t tid;                    //定义线程描述符
  20.     pthread_attr_t thread_attr;            //定义线程属性对像
  21.     
  22.     pthread_attr_init(&thread_attr);                //线程属性初始化
  23.     pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);    //将线程设置为分离状态

  24.     if(pthread_create(&tid, &thread_attr, mythread, NULL))    //创建新线程,并修改属性
  25.     {
  26.         printf("pthread_create error\n");
  27.         exit(0);
  28.     }
  29.     
  30.     while(thread_flag)
  31.     {
  32.         printf("Wait for thread finished\n");
  33.         sleep(1);
  34.     }
  35.     printf("Thread finish\n");
  36.     return 0;
  37. }
  1. /******************************************************************************
  2. * FILE: bug2.c
  3. * DESCRIPTION:
  4. * A "hello world" Pthreads program that dumps core. Figure out why and
  5. * then fix it.
  6. * 修改之后的文件
  7. ******************************************************************************/
  8. #include <pthread.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>

  11. #define NTHREADS    8
  12. #define ARRAY_SIZE 5000000

  13. void * Hello(void * threadid)
  14. {
  15.     double A[ARRAY_SIZE];
  16.     int i;
  17.     long tid;
  18.     
  19.     tid = (long)threadid;
  20.     sleep(3);
  21.     for (i=0; i<ARRAY_SIZE; i++)
  22.     {
  23.         A[i] = i * 1.0;
  24.     }
  25.     printf("%ld: Hello World! %f\n", tid, A[ARRAY_SIZE-1]);
  26.     pthread_exit(NULL);
  27. }

  28. int main(int argc, char *argv[])
  29. {
  30.     pthread_t threads[NTHREADS];
  31.     size_t stacksize;
  32.     pthread_attr_t attr;
  33.     int rc;
  34.     long t;
  35.     
  36.     pthread_attr_init(&attr);
  37.     pthread_attr_setstacksize(&attr, sizeof(double)*ARRAY_SIZE + 10485760);
  38.     pthread_attr_getstacksize(&attr, &stacksize);
  39.     printf("Thread stack size = %li bytes (hint, hint)\n",stacksize);
  40.     for(t=0;t<NTHREADS;t++)
  41.     {
  42.         rc = pthread_create(&threads[t], &attr, Hello, (void *)t);
  43.         if (rc)
  44.         {
  45.             printf("ERROR; return code from pthread_create() is %d\n", rc);
  46.             exit(-1);
  47.         }
  48.     }
  49.     printf("Created %ld threads.\n", t);
  50.     for(t=0;t<NTHREADS;t++)
  51.     {
  52.         rc = pthread_join(threads[t], NULL);
  53.         if(rc)
  54.         {
  55.             printf("ERROR; return code from pthread_join() is %d\n", rc);
  56.             exit(-1);
  57.         }
  58.     }
  59.     pthread_attr_destroy(&attr);
  60.     return 0;
  61. }
阅读(161) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~