Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7540647
  • 博文数量: 961
  • 博客积分: 15795
  • 博客等级: 上将
  • 技术积分: 16612
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 14:23
文章分类

全部博文(961)

文章存档

2016年(1)

2015年(61)

2014年(41)

2013年(51)

2012年(235)

2011年(391)

2010年(181)

分类: LINUX

2011-06-18 19:57:29

  1. /*
  2.  * 线程是把进程分成若干个可执行的线程,而每一个线程都独立运行
  3.  * 线程运行于进程空间之中,它是进程内部的一个执行单元
  4.  * 创建线程
  5.  * Lzy 2011-6-18
  6.  */

  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <pthread.h>

  10. void *mythread(void *arg)        //新线程运行的函数
  11. {
  12.     printf("hello\n");
  13. }

  14. int main(void)
  15. {
  16.     pthread_t tid;        //定义线程标识符
  17.     
  18.     if(pthread_create(&tid, NULL, mythread, NULL))            //创建线程
  19.     {
  20.         printf("%s error %d line", __FUNCTION__, __LINE__);        //打印错误信息
  21.         exit(0);
  22.     }
  23.     else
  24.     {
  25.         sleep(1);                
  26.     }
  27.     return 0;
  28. }
  1. /*
  2.  * 线程参数传递
  3.  * Lzy 2011-6-18
  4.  */

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

  8. void *thread(void *arg)
  9. {
  10.     printf("%d\n",*((int *)arg));
  11.     pthread_exit(NULL);
  12. }
  13. int main(void)
  14. {
  15.     int i;    
  16.     pthread_t tid;
  17.     
  18.     for(i = 0; i < 10; i++)
  19.     {
  20.         pthread_create(&tid, NULL, thread, &i);        
  21.     }
  22.     
  23.     pthread_exit(NULL);
  24.     return 0;
  25. }
  1. /*
  2.  * 线程合并
  3.  * Lzy 2011-6-18
  4.  */

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

  8. void *mythread(void *arg)        //新线程运行的函数
  9. {
  10.     printf("hello\n");
  11. }

  12. int main(void)
  13. {
  14.     pthread_t tid;        //定义线程标识符
  15.     
  16.     if(pthread_create(&tid, NULL, mythread, NULL))            //创建线程
  17.     {
  18.         printf("%s error %d line", __FUNCTION__, __LINE__);        //打印错误信息
  19.         exit(0);
  20.     }
  21.     else
  22.     {
  23.         pthread_join(tid, NULL);    //等待新线程结束                
  24.     }
  25.     return 0;
  26. }
  1. /*
  2.  * 线程退出
  3.  * Lzy 2011-6-18
  4.  */

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

  8. void *mythread(void *arg)        //新线程运行的函数
  9. {
  10.     printf("hello\n");
  11.     pthread_exit("pthread exit");    //线程退出
  12. }

  13. int main(void)
  14. {
  15.     pthread_t tid;        //定义线程标识符
  16.     void *str;
  17.     
  18.     if(pthread_create(&tid, NULL, mythread, NULL))            //创建线程
  19.     {
  20.         printf("%s error %d line", __FUNCTION__, __LINE__);        //打印错误信息
  21.         exit(0);
  22.     }
  23.     else
  24.     {
  25.         pthread_join(tid, &str);    //等待新线程结束
  26.         printf("%s\n",(char *)str);    //输出线程退出状态
  27.     }    
  28.     
  29.     return 0;
  30. }
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. void mythread(void)                    /* 定义新线程运行的函数 */
  5. {
  6.   int i;
  7.   for(i=0; i<3; i++)                    /* 连续输出字符串 */
  8.   {
  9.     printf("This is a pthread.\n");
  10.   }
  11.   /* 终止当前线程,并返回一个指向字符串的指针 */
  12.   pthread_exit("Thank you for the CPU time.\n");
  13. }
  14. int main(void)
  15. {
  16.   pthread_t id;                        /* 定义线程的标识符 */
  17.   int i, ret;
  18.   void *thread_result;                /* 定义指针,用来存储线程的返回值 */
  19.   ret = pthread_create(&id, NULL, (void *)mythread, NULL);            /* 创建新的线程 */
  20.   if(ret != 0)
  21.   {
  22.     printf ("Create pthread error.\n");        /* 如果线程创建失败,打印错误信息 */
  23.     exit (1);
  24.   }
  25.   for(i=0;i<3;i++)                    /* 连续输出字符串 */
  26.   {
  27.     printf("This is the main process.\n");
  28.   }
  29.   /* 主线程阻塞,等待新建线程返回,并将返回值存储在前面定义的thread_result之中 */
  30.   pthread_join(id,&thread_result);
  31.   printf("Thread joined, it returned: %s", (char *)thread_result);    /* 输出线程返回的字符串 */
  32.   return 0;
  33. }
  1. /*
  2.  * 终止其他线程
  3.  * 被终止的线程属性必须设置为可被终止
  4.  * Lzy 2011-6-18
  5.  */

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

  9. void *mythread(void *arg)        //新线程运行的函数
  10. {
  11.     int i;
  12.     
  13.     if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL))    //设置线程取消状态
  14.     {
  15.         printf("FUNC %s: %d\n", __FUNCTION__, __LINE__);    //打印错误信息
  16.         pthread_exit("pthread_setcancelstate");
  17.     }
  18.     
  19.     if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL))    //设置线程取消类型
  20.     {
  21.         printf("FUNC %s: %d\n", __FUNCTION__, __LINE__);    //打印错误信息
  22.         pthread_exit("pthread_setcanceltype");
  23.     }
  24.     
  25.     for(i = 0; i < 10; i++)
  26.     {
  27.         printf("Thread is running(%d)\n",i);
  28.         sleep(1);
  29.     }
  30.     
  31.     pthread_exit("thread exit");    //线程退出
  32. }

  33. int main(void)
  34. {
  35.     pthread_t tid;        //定义线程标识符
  36.     void *pthread_result = "";
  37.     
  38.     
  39.     if(pthread_create(&tid, NULL, mythread, NULL))        //创建新线程
  40.     {
  41.         printf("FUNC %s: %d\n", __FUNCTION__, __LINE__);    //打印错误信息
  42.         exit(0);
  43.     }
  44.     sleep(3);            //让新创建的线程运行一段时间
  45.     
  46.     printf("cancel thread-----\n");
  47.     if(pthread_cancel(tid))            //终止新线程
  48.     {        
  49.         printf("FUNC %s: %d\n", __FUNCTION__, __LINE__);    //打印错误信息
  50.         exit(0);    
  51.     }
  52.     
  53.     pthread_join(tid, NULL);        //等待新线程退出
  54.     printf("thread join, it returned\n");
  55.     
  56.     return 0;
  57. }
阅读(1463) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~