[并发并行]_[pthread]_[对工作线程进行简单控制-暂停-继续-停止]


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


界面和Win32版本的:

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


场景:

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

2.pthread优点:跨平台。


耗时:3h


文件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.\n",thread_data->file);

}

void * StartPthread(void * arg)
{
    DhThreadData* data = (DhThreadData*)arg;
    WriteLine("StartPthread begin scan.\n",data->file);

    data->thread_status = kRunning;
    while(true)
    {
        WriteLine("StartPthread scanning.\n",data->file,false);
        //1.暂停
        if(data->thread_status == kPause)
        {
            WriteLine("StartPthread pause thread.\n",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.\n",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.\n",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........\n",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.开始扫描.\n2.继续扫描->暂停扫描切换.\n3.停止扫描.\nEnter q for exit:.\n",file);
    while(1)
    {
        i = getchar();
        if(i == '\n')
        {
            continue;
        }

        if(i == 'q')
        {
            if(thread_data.thread_status != kStop)
            {
                StopThread(&thread_data);
            }
            WriteLine("Exit.\n",file);
            break;
        }else if(i == '1')
        {
            if(thread_data.thread_status == kStop)
            {
                StartScan(&thread_data);
            }else
            {
                WriteLine("Scanning.\n",file);
                WriteLine("Continue response key press.\n",file);
                continue;
            }
        }else if(i == '2')
        {
            if(thread_data.thread_status == kStop)
            {
                WriteLine("Press 1 for begin scanning.\n",file);
                WriteLine("Continue response key press.\n",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.\n",file);
            continue;
        }else if (i == '3')
        {
            if(thread_data.thread_status == kStop)
            {
                WriteLine("Scan has stopped.\n",file);
                WriteLine("Continue response key press.\n",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...........\n",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.





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Peter(阿斯拉达)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值