[POSIX线程模型]_[使用pthread对工作线程进行简单控制-暂停-继续-停止]

1.使用命令行对线程简单控制。


界面和Win32版本的:

http://blog.csdn.net/xianglitian/article/details/6729851


场景:

1.在开发界面程序时,为了不让界面程序挂起,会新建一个工作线程来处理逻辑,主线程就能持续和用户交互。

2.pthread优点:跨平台。


耗时:3h


文件1: test_pthread.cpp

  1. #include <stdio.h>  
  2. #include "pthread.h"  
  3. #include <iostream>  
  4.   
  5. using namespace std;  
  6.   
  7. enum DhThreadStatus  
  8. {  
  9.     kStop,  
  10.     kRunning,  
  11.     kPause  
  12. };  
  13.   
  14. typedef struct DhThreadData  
  15. {  
  16.     int thread_status;  
  17.   
  18.     pthread_mutex_t mutex;//共享互斥量  
  19.   
  20.     //1.控制工作线程.  
  21.     pthread_cond_t work_cond;//条件变量  
  22.   
  23.     //1.控制主线程.  
  24.     pthread_cond_t main_cond;//条件变量  
  25.   
  26.     FILE* file;//记录日志  
  27.   
  28. }DhThreadData;  
  29.   
  30. void WriteLine(const char* str,FILE* file,bool is_print_console = true)  
  31. {  
  32.     if(is_print_console)  
  33.     {  
  34.         printf("%s",str);  
  35.     }  
  36.     size_t log_length = strlen(str);  
  37.     fwrite(str,log_length,1,file);  
  38.     fflush(file);  
  39. }  
  40.   
  41. void StopThread(DhThreadData* thread_data)  
  42. {  
  43.     //call stop  
  44.     //如果是扫描或暂停状态,通知线程停止扫描.  
  45.     //等待线程结束.  
  46.     pthread_mutex_lock(&thread_data->mutex);  
  47.     thread_data->thread_status = kStop;  
  48.     pthread_cond_signal(&thread_data->work_cond);//1.告诉工作线程可以继续了.  
  49.     pthread_cond_wait(&thread_data->main_cond,&thread_data->mutex);//2.等待  
  50.     pthread_mutex_unlock(&thread_data->mutex);  
  51.     WriteLine("Continue response key press.\n",thread_data->file);  
  52.   
  53. }  
  54.   
  55. void * StartPthread(void * arg)  
  56. {  
  57.     DhThreadData* data = (DhThreadData*)arg;  
  58.     WriteLine("StartPthread begin scan.\n",data->file);  
  59.   
  60.     data->thread_status = kRunning;  
  61.     while(true)  
  62.     {  
  63.         WriteLine("StartPthread scanning.\n",data->file,false);  
  64.         //1.暂停  
  65.         if(data->thread_status == kPause)  
  66.         {  
  67.             WriteLine("StartPthread pause thread.\n",data->file);  
  68.             pthread_mutex_lock(&data->mutex);  
  69.             pthread_cond_signal(&data->main_cond);  
  70.             pthread_cond_wait(&data->work_cond,&data->mutex);  
  71.             pthread_mutex_unlock(&data->mutex);  
  72.             //1.返回时,状态是kRunning或者kStop.  
  73.             if(data->thread_status == kRunning)  
  74.             {  
  75.                 //1.告诉主线程继续.  
  76.                 WriteLine("StartPthread continue thread.\n",data->file);  
  77.                 pthread_cond_signal(&data->main_cond);  
  78.             }  
  79.         }  
  80.         if(data->thread_status == kStop)  
  81.         {  
  82.             pthread_mutex_lock(&data->mutex);  
  83.             pthread_cond_signal(&data->main_cond);  
  84.             pthread_mutex_unlock(&data->mutex);  
  85.             WriteLine("StartPthread Stop.\n",data->file);  
  86.             break;  
  87.         }  
  88.         Sleep(500);  
  89.     }  
  90.     //1.告诉主线程继续,工作线程已结束.  
  91.     return NULL;  
  92. }  
  93.   
  94. void StartScan(DhThreadData* data)  
  95. {  
  96.     pthread_t t1;  
  97.     pthread_create(&t1, NULL, StartPthread, data);  
  98.     pthread_detach(t1);  
  99. }  
  100.   
  101. int main(int argc, char *argv[])  
  102. {  
  103.     setbuf(stdout, (char*) 0);  
  104.     setbuf(stderr, (char*) 0);  
  105.     FILE* file = fopen("log.txt","w");  
  106.     WriteLine("begin........\n",file);  
  107.   
  108.     DhThreadData thread_data;  
  109.     memset(&thread_data,0,sizeof(thread_data));  
  110.     thread_data.file = file;  
  111.     pthread_mutex_init(&thread_data.mutex,NULL);  
  112.     pthread_cond_init(&thread_data.work_cond,NULL);  
  113.   
  114.     pthread_cond_init(&thread_data.main_cond,NULL);  
  115.   
  116.     int i;  
  117.     WriteLine("1.开始扫描.\n2.继续扫描->暂停扫描切换.\n3.停止扫描.\nEnter q for exit:.\n",file);  
  118.     while(1)  
  119.     {  
  120.         i = getchar();  
  121.         if(i == '\n')  
  122.         {  
  123.             continue;  
  124.         }  
  125.   
  126.         if(i == 'q')  
  127.         {  
  128.             if(thread_data.thread_status != kStop)  
  129.             {  
  130.                 StopThread(&thread_data);  
  131.             }  
  132.             WriteLine("Exit.\n",file);  
  133.             break;  
  134.         }else if(i == '1')  
  135.         {  
  136.             if(thread_data.thread_status == kStop)  
  137.             {  
  138.                 StartScan(&thread_data);  
  139.             }else  
  140.             {  
  141.                 WriteLine("Scanning.\n",file);  
  142.                 WriteLine("Continue response key press.\n",file);  
  143.                 continue;  
  144.             }  
  145.         }else if(i == '2')  
  146.         {  
  147.             if(thread_data.thread_status == kStop)  
  148.             {  
  149.                 WriteLine("Press 1 for begin scanning.\n",file);  
  150.                 WriteLine("Continue response key press.\n",file);  
  151.                 continue;  
  152.             }  
  153.             //如果是暂停状态,通知线程继续扫描.  
  154.             if(thread_data.thread_status == kPause)  
  155.             {  
  156.                 //继续  
  157.                 pthread_mutex_lock(&thread_data.mutex);  
  158.                 thread_data.thread_status = kRunning;  
  159.                 pthread_cond_signal(&thread_data.work_cond);//1.告诉工作线程可以继续了.  
  160.                 pthread_cond_wait(&thread_data.main_cond,&thread_data.mutex);//2.等待  
  161.                 pthread_mutex_unlock(&thread_data.mutex);  
  162.             }else  
  163.             {  
  164.                 //暂停  
  165.                 pthread_mutex_lock(&thread_data.mutex);  
  166.                 thread_data.thread_status = kPause;  
  167.                 pthread_cond_wait(&thread_data.main_cond,&thread_data.mutex);  
  168.                 pthread_mutex_unlock(&thread_data.mutex);  
  169.                   
  170.             }  
  171.             WriteLine("Continue response key press.\n",file);  
  172.             continue;  
  173.         }else if (i == '3')  
  174.         {  
  175.             if(thread_data.thread_status == kStop)  
  176.             {  
  177.                 WriteLine("Scan has stopped.\n",file);  
  178.                 WriteLine("Continue response key press.\n",file);  
  179.                 continue;  
  180.             }  
  181.             StopThread(&thread_data);  
  182.         }  
  183.     }  
  184.       
  185.     pthread_mutex_destroy(&thread_data.mutex);  
  186.     pthread_cond_destroy(&thread_data.work_cond);  
  187.   
  188.     pthread_cond_destroy(&thread_data.main_cond);  
  189.     fclose(file);  
  190.     WriteLine("end...........\n",file);  
  191.     return 0;  
  192. }  

输出:
[plain] view plain copy
  1. begin........  
  2. 1.开始扫描.  
  3. 2.继续扫描->暂停扫描切换.  
  4. 3.停止扫描.  
  5. Enter q for exit:.  
  6. StartPthread begin scan. //1  
  7. StartPthread scanning.  
  8. StartPthread scanning.  
  9. StartPthread scanning.  
  10. StartPthread scanning.  
  11. StartPthread scanning.  
  12. StartPthread scanning.  
  13. StartPthread pause thread. //2  
  14. Continue response key press.  
  15. StartPthread continue thread. //2  
  16. Continue response key press.  
  17. StartPthread scanning.  
  18. StartPthread scanning.  
  19. StartPthread scanning.  
  20. StartPthread scanning.  
  21. StartPthread pause thread. //2  
  22. Continue response key press.  
  23. StartPthread continue thread. //2  
  24. Continue response key press.  
  25. StartPthread scanning.  
  26. StartPthread scanning.  
  27. StartPthread scanning.  
  28. StartPthread scanning.  
  29. StartPthread scanning.  
  30. StartPthread scanning.  
  31. StartPthread scanning.  
  32. StartPthread Stop. //3  
  33. Continue response key press.  
  34. StartPthread begin scan. //1  
  35. StartPthread scanning.  
  36. StartPthread scanning.  
  37. StartPthread scanning.  
  38. StartPthread scanning.  
  39. StartPthread scanning.  
  40. StartPthread scanning.  
  41. StartPthread scanning.  
  42. StartPthread scanning.  
  43. StartPthread scanning.  
  44. StartPthread scanning.  
  45. StartPthread scanning.  
  46. StartPthread scanning.  
  47. StartPthread scanning.  
  48. StartPthread scanning.  
  49. StartPthread scanning.  
  50. StartPthread scanning.  
  51. StartPthread pause thread. //2  
  52. Continue response key press.  
  53. StartPthread Stop. //3  
  54. Continue response key press.  
  55. StartPthread begin scan. //1  
  56. StartPthread scanning.  
  57. StartPthread scanning.  
  58. StartPthread scanning.  
  59. StartPthread scanning.  
  60. StartPthread scanning.  
  61. StartPthread scanning.  
  62. StartPthread scanning.  
  63. StartPthread scanning.  
  64. StartPthread scanning.  
  65. StartPthread scanning.  
  66. StartPthread Stop. //3  
  67. Continue response key press.  
  68. StartPthread begin scan. //1  
  69. StartPthread scanning.  
  70. StartPthread scanning.  
  71. StartPthread scanning.  
  72. StartPthread scanning.  
  73. StartPthread Stop. //q  
  74. Continue response key press.  
  75. Exit. 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值