第四期 pthread_cancel()

简介: /* Name thread_create.c  * Author dyli  * date 20110615  * Description 处理getpid()、pthread_self()、pthread_create()函数,  *                ...
  1. /* Name thread_create.c
  2.  * Author dyli
  3.  * date 20110615
  4.  * Description 处理getpid()、pthread_self()、pthread_create()函数,
  5.  *                并进行讨论主子线程的问题
  6.  * */


  7. /***pthread_cancel()函数************************************************
  8. *    函数功能:    可以(在其它线程中)结束一个线程
  9. *    函数原型:
  10. *    返回值 :    返回值为0时,创建成功!!
  11. *                
  12. *    所需库 :    
  13. * 备注 :    
  14. ****************************************************************/

  15. #include pthread.h>
  16. #include stdio.h>
  17. #include stdlib.h>

  18. int i=0;

  19. void *thread_func(void *argc)
  20. {
  21.     int *val=argc;
  22.     printf("Hi,man! I'm a son Thread\n\n");
  23.     if(argc!=NULL)
  24.     {
  25.         //for(int i=0;i1000;i++)
  26.         //c99不允许在for循环里初始化,会弹出在 C99 模式之外使用‘for’循环初始化声明错误
  27.         
  28.         for(;i50;i++)
  29.         {
  30.             ++i; //这两个不经意的++,产生了50内顺序显示奇数的效果
  31.             printf("Now Show you the message:%d\n",*val);
  32.             printf("Thread run %d times",i);
  33.         }
  34.     }
  35. }

  36. int main()
  37. {
  38.     pthread_t tid;
  39.     int t_arg =10;
  40.     int err;
  41.     err=pthread_create(&tid,NULL,thread_func,&t_arg); //--->带参数的pthread_create()函数
  42.     if(err)//返回值为0时,创建成功!!
  43.     {     
  44.         printf("create thread error!!\n");    
  45.     }
  46.     
  47.     sleep(1);
  48.     printf("run in main Thread\n");
  49.     if(pthread_cancel(tid))//---------------------------->func1结束线程
  50.     {
  51.         printf("向id为tid的线程发送了取消执行,但并不表示该线程会停止执行\n");
  52.     }
  53.     return 0;
  54. }
  1. [root@localhost thread_cancel]# ./thread_cancel
    Hi,man! I'm a son Thread
  2. Now Show you the message:10
    Thread run 1 timesNow Show you the message:10
    Thread run 3 timesNow Show you the message:10
    Thread run 5 timesNow Show you the message:10
    Thread run 7 timesNow Show you the message:10
    Thread run 9 timesNow Show you the message:10
    Thread run 11 timesNow Show you the message:10
    Thread run 13 timesNow Show you the message:10
    Thread run 15 timesNow Show you the message:10
    Thread run 17 timesNow Show you the message:10
    Thread run 19 timesNow Show you the message:10
    Thread run 21 timesNow Show you the message:10
    Thread run 23 timesNow Show you the message:10
    Thread run 25 timesNow Show you the message:10
    Thread run 27 timesNow Show you the message:10
    Thread run 29 timesNow Show you the message:10
    Thread run 31 timesNow Show you the message:10
    Thread run 33 timesNow Show you the message:10
    Thread run 35 timesNow Show you the message:10
    Thread run 37 timesNow Show you the message:10
    Thread run 39 timesNow Show you the message:10
    Thread run 41 timesNow Show you the message:10
    Thread run 43 timesNow Show you the message:10
    Thread run 45 timesNow Show you the message:10
    Thread run 47 timesNow Show you the message:10
    Thread run 49 timesrun in main Thread
    向id为tid的线程发送了取消执行,但并不表示该线程会停止执行
    [root@localhost thread_cancel]#

 

 

  1. if(argc!=NULL)
  2.     {
  3.          for(int i=0;i1000;i++)
  4.         //c99不允许在for循环里初始化,会弹出在 C99 模式之外使用‘for’循环初始化声明错误        
  5.         //for(;i50;i++)
  6.         {
  7.             ++i; //这两个不经意的++,产生了50内顺序显示奇数的效果
  8.             printf("Now Show you the message:%d\n",*val);
  9.             printf("Thread run %d times",i);
  10.         }
  11.     }

 

 

  1. [root@localhost thread_cancel]# make
  2. cc -c -o thread_cancel.o thread_cancel.c
  3. thread_cancel.c: 在函数‘thread_func’中:
  4. thread_cancel.c:30: 错误:在 C99 模式之外使用‘for’循环初始化声明
  5. make: *** [thread_cancel.o] 错误 1
相关文章
|
4月前
条件变量函数pthread_cond_timedwait实现业务场景
条件变量函数pthread_cond_timedwait实现业务场景
34 0
|
3月前
|
存储 缓存 安全
C语言进程(第二章,wait,sleep,waitpid,pthread_mutex_lock,pthread_mutex_unlock)
C语言进程(第二章,wait,sleep,waitpid,pthread_mutex_lock,pthread_mutex_unlock)
27 0
|
10月前
|
存储 固态存储 Linux
深入学习tombstone和signal
深入学习tombstone和signal
125 0
深入学习tombstone和signal
|
JavaScript 前端开发 API
事件循环机制(Event Loop)的基本认知
事件循环机制(Event Loop)的基本认知
251 0
事件循环机制(Event Loop)的基本认知
|
物联网 Linux 开发者
Pthread_cancel 线程取消|学习笔记
快速学习 Pthread_cancel 线程取消
86 0
|
调度
求求你,别再用wait和notify了!(4)
求求你,别再用wait和notify了!(4)
78 0
求求你,别再用wait和notify了!(4)
|
Java
求求你,别再用wait和notify了!(1)
Condition 是 JDK 1.5 中提供的用来替代 wait 和 notify 的线程通讯方法,那么一定会有人问:为什么不能用 wait 和 notify 了? 哥们我用的好好的。老弟别着急,听我给你细说... 之所以推荐使用 Condition 而非 Object 中的 wait 和 notify 的原因有两个: 使用 notify 在极端环境下会造成线程“假死”; Condition 性能更高。 接下来怎们就用代码和流程图的方式来演示上述的两种情况。
93 0
求求你,别再用wait和notify了!(1)
求求你,别再用wait和notify了!(2)
求求你,别再用wait和notify了!(2)
89 0
求求你,别再用wait和notify了!(2)
求求你,别再用wait和notify了!(3)
求求你,别再用wait和notify了!(3)
77 0
求求你,别再用wait和notify了!(3)