CPP线程的停止与回收

 1 #define _CRT_SECURE_NO_WARNINGS /* VS2013,2015需要这一行 */
 2 #include <stdio.h>
 3 
 4 #include "osapi/osapi.h"
 5 
 6 // 定义一个类
 7 class Buddhist : public OS_Thread
 8 {
 9 public:
10     int Start()  //封装线程开始函数
11     {
12         // 其他准备工作
13         m_quitflag = false;  //设置标示量为假
14         Run();
15         return 0;
16     }
17 
18     void Stop()  //封装线程结束函数
19     {
20         m_quitflag = true;  //设置标示量为真
21         Join(this);  //回收线程
22     }
23 
24 private:
25     // 线程主函数
26     virtual int Routine()
27     {
28         // 线程体: 执行它的任务
29         for(int i=0; !m_quitflag && i<100; i++)  //执行100次并且标示量不为则真执行
30         {
31             printf("ma mi ma mi hong ...\n");
32             OS_Thread::Sleep(1);
33         }
34         printf("Task Exit.\n");
35         //  保存数据,善后工作
36         printf("善后工作...\n");
37         return 0; // 正常退出
38     }
39 
40 private:
41     bool m_quitflag;  //定义标示量
42 
43 };
44 
45 int main()
46 {
47     Buddhist  task1;  //创建线程
48     task1.Start();  //运行线程
49 
50     getchar();
51     task1.Stop();  //停止线程
52 
53     return 0;
54 }

 

posted on 2017-12-15 23:39  Doctor_uee  阅读(358)  评论(0)    收藏  举报

导航