[同時並行][pthread]_[ワークスレッドの簡単な制御-一時停止-続行-停止]


1.コマンドラインを使用してスレッドを簡単に制御します.
インタフェースとWin 32のバージョン:
http://blog.csdn.net/xianglitian/article/details/6729851
シーン:
1.インタフェースプログラムを開発する際、インタフェースプログラムを停止させないように、ロジックを処理するためにワークスレッドを新規作成し、メインスレッドはユーザーと対話し続けることができます.
2.pthreadの利点:プラットフォームにまたがる.
消費時間:3 h
ファイル1:test_pthread.cpp
#include <stdio.h>
#include "pthread.h"
#include <iostream>

using namespace std;

enum DhThreadStatus
{
    kStop,
    kRunning,
    kPause
};

typedef struct DhThreadData
{
    int thread_status;

    pthread_mutex_t mutex;//     

    //1.      .
    pthread_cond_t work_cond;//    

    //1.     .
    pthread_cond_t main_cond;//    

    FILE* file;//    

}DhThreadData;

void WriteLine(const char* str,FILE* file,bool is_print_console = true)
{
    if(is_print_console)
    {
        printf("%s",str);
    }
    size_t log_length = strlen(str);
    fwrite(str,log_length,1,file);
    fflush(file);
}

void StopThread(DhThreadData* thread_data)
{
    //call stop
    //          ,        .
    //      .
    pthread_mutex_lock(&thread_data->mutex);
    thread_data->thread_status = kStop;
    pthread_cond_signal(&thread_data->work_cond);//1.           .
    pthread_cond_wait(&thread_data->main_cond,&thread_data->mutex);//2.  
    pthread_mutex_unlock(&thread_data->mutex);
    WriteLine("Continue response key press.
",thread_data->file); } void * StartPthread(void * arg) {     DhThreadData* data = (DhThreadData*)arg;     WriteLine("StartPthread begin scan.
",data->file);     data->thread_status = kRunning;     while(true)     {         WriteLine("StartPthread scanning.
",data->file,false);         //1.         if(data->thread_status == kPause)         {             WriteLine("StartPthread pause thread.
",data->file);             pthread_mutex_lock(&data->mutex);             pthread_cond_signal(&data->main_cond);             pthread_cond_wait(&data->work_cond,&data->mutex);             pthread_mutex_unlock(&data->mutex);             //1. , kRunning kStop.             if(data->thread_status == kRunning)             {                 //1. .                 WriteLine("StartPthread continue thread.
",data->file);                 pthread_cond_signal(&data->main_cond);             }         }         if(data->thread_status == kStop)         {             pthread_mutex_lock(&data->mutex);             pthread_cond_signal(&data->main_cond);             pthread_mutex_unlock(&data->mutex);             WriteLine("StartPthread Stop.
",data->file);             break;         }         Sleep(500);     }     //1. , .     return NULL; } void StartScan(DhThreadData* data) {     pthread_t t1;     pthread_create(&t1, NULL, StartPthread, data);     pthread_detach(t1); } int main(int argc, char *argv[]) {     setbuf(stdout, (char*) 0);     setbuf(stderr, (char*) 0);     FILE* file = fopen("log.txt","w");     WriteLine("begin........
",file);     DhThreadData thread_data;     memset(&thread_data,0,sizeof(thread_data));     thread_data.file = file;     pthread_mutex_init(&thread_data.mutex,NULL);     pthread_cond_init(&thread_data.work_cond,NULL);     pthread_cond_init(&thread_data.main_cond,NULL);     int i;     WriteLine("1. .
2. -> .
3. .
Enter q for exit:.
",file);     while(1)     {         i = getchar();         if(i == '
')         {             continue;         }         if(i == 'q')         {             if(thread_data.thread_status != kStop)             {                 StopThread(&thread_data);             }             WriteLine("Exit.
",file);             break;         }else if(i == '1')         {             if(thread_data.thread_status == kStop)             {                 StartScan(&thread_data);             }else             {                 WriteLine("Scanning.
",file);                 WriteLine("Continue response key press.
",file);                 continue;             }         }else if(i == '2')         {             if(thread_data.thread_status == kStop)             {                 WriteLine("Press 1 for begin scanning.
",file);                 WriteLine("Continue response key press.
",file);                 continue;             }             // , .             if(thread_data.thread_status == kPause)             {                 //                 pthread_mutex_lock(&thread_data.mutex);                 thread_data.thread_status = kRunning;                 pthread_cond_signal(&thread_data.work_cond);//1. .                 pthread_cond_wait(&thread_data.main_cond,&thread_data.mutex);//2.                 pthread_mutex_unlock(&thread_data.mutex);             }else             {                 //                 pthread_mutex_lock(&thread_data.mutex);                 thread_data.thread_status = kPause;                 pthread_cond_wait(&thread_data.main_cond,&thread_data.mutex);                 pthread_mutex_unlock(&thread_data.mutex);                              }             WriteLine("Continue response key press.
",file);             continue;         }else if (i == '3')         {             if(thread_data.thread_status == kStop)             {                 WriteLine("Scan has stopped.
",file);                 WriteLine("Continue response key press.
",file);                 continue;             }             StopThread(&thread_data);         }     }          pthread_mutex_destroy(&thread_data.mutex);     pthread_cond_destroy(&thread_data.work_cond);     pthread_cond_destroy(&thread_data.main_cond);     fclose(file);     WriteLine("end...........
",file);     return 0; }

出力:
begin........
1.    .
2.    ->      .
3.    .
Enter q for exit:.
StartPthread begin scan. //1
StartPthread scanning.
StartPthread scanning.
StartPthread scanning.
StartPthread scanning.
StartPthread scanning.
StartPthread scanning.
StartPthread pause thread. //2
Continue response key press.
StartPthread continue thread. //2
Continue response key press.
StartPthread scanning.
StartPthread scanning.
StartPthread scanning.
StartPthread scanning.
StartPthread pause thread. //2
Continue response key press.
StartPthread continue thread. //2
Continue response key press.
StartPthread scanning.
StartPthread scanning.
StartPthread scanning.
StartPthread scanning.
StartPthread scanning.
StartPthread scanning.
StartPthread scanning.
StartPthread Stop. //3
Continue response key press.
StartPthread begin scan. //1
StartPthread scanning.
StartPthread scanning.
StartPthread scanning.
StartPthread scanning.
StartPthread scanning.
StartPthread scanning.
StartPthread scanning.
StartPthread scanning.
StartPthread scanning.
StartPthread scanning.
StartPthread scanning.
StartPthread scanning.
StartPthread scanning.
StartPthread scanning.
StartPthread scanning.
StartPthread scanning.
StartPthread pause thread. //2
Continue response key press.
StartPthread Stop. //3
Continue response key press.
StartPthread begin scan. //1
StartPthread scanning.
StartPthread scanning.
StartPthread scanning.
StartPthread scanning.
StartPthread scanning.
StartPthread scanning.
StartPthread scanning.
StartPthread scanning.
StartPthread scanning.
StartPthread scanning.
StartPthread Stop. //3
Continue response key press.
StartPthread begin scan. //1
StartPthread scanning.
StartPthread scanning.
StartPthread scanning.
StartPthread scanning.
StartPthread Stop. //q
Continue response key press.
Exit.