• 线程启动、暂停与挂起


    ////

    ////  main.cpp

    ////  ResumeTest

    ////

    ////  Created by zhangguangming1 on 2019/5/12.

    ////  Copyright © 2019 zhangguangming1. All rights reserved.

    ////

    //

    #include <iostream>

    #include <stdio.h>

    #include <string.h>

    #include <string>

    #include <curl/curl.h>

    #include <unistd.h>

    #include<fstream>

    #include "curl_http.h"

    #include "md5.h"

    #include <thread>

    #include <termios.h>

    //

    //

    #define RUN 1

    #define STOP 0

    typedef struct{

        string url;

        string fileMD5;

        string targetname;

        string savepath;

    }*pFileInfo,FileInfo;

    pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;

    pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

    int status = STOP;

    std::string getFileMd5(string file) {

        if (0!=access(file.c_str(),0)) {

            return "";

        }

        std::ifstream filestream(file, std::ios::binary|std::ios::in);

        MD5 md5;

        md5.update(filestream);

        return md5.toString();

    }

    int scanKeyboard()

    {

        int in;

        struct termios new_settings;

        struct termios stored_settings;

        tcgetattr(0,&stored_settings);

        

        new_settings = stored_settings;

        new_settings.c_lflag &= (~ICANON);

        new_settings.c_cc[VTIME] = 0;

        tcgetattr(0,&stored_settings);

        new_settings.c_cc[VMIN] = 1;

        tcsetattr(0,TCSANOW,&new_settings);

        

        in = getchar();

        

        tcsetattr(0,TCSANOW,&stored_settings);

        return in;

    }

    void * thread_function(void*arg)

    {

        pthread_mutex_lock(&mut);

        while (!status)

        {

            pthread_cond_wait(&cond, &mut);

        }

        pthread_mutex_unlock(&mut);

        pFileInfo info = (pFileInfo)arg;

        curl_http_downloader dwcurl(info->targetname);

        dwcurl.Download(info->url, info->savepath, nullptr,info->fileMD5);

        return NULL;

    }

    void thread_resume()

    {

        if (status == STOP)

        {

            pthread_mutex_lock(&mut);

            status = RUN;

            pthread_cond_signal(&cond);

            printf("pthread run! ");

            pthread_mutex_unlock(&mut);

        }

        else

        {

            printf("pthread run already ");

        }

    }

    void thread_pause()

    {

        if (status == RUN)

        {

            pthread_mutex_lock(&mut);

            status = STOP;

            printf("thread stop! ");

            pthread_mutex_unlock(&mut);

        }

        else

        {

            printf("pthread pause already ");

        }

    }

    int main(int argc, const char * argv[]) {

    //    // insert code here...

    //      http://192.168.34.72/pc_client/teacher-dev-mac/output/vipkid_mac_100/VIPKIDT-v1.7.5-mac-x64.dmg

    //    string url = "http://192.168.34.72/pc_client/teacher-dev-mac-qt/output/vipkid_mac_10/VIPKIDT-v-mac-x64.dmg";

    //    string CurrectMD5 = getFileMd5("./VIPKIDT-v-mac-x64.dmg");

    //    string name = "VIPKIDT.dmg";

    //    string path = "./";

    //    curl_http_downloader dwcurl(name);

    //    dwcurl.Download(url, path, nullptr,CurrectMD5);

        

         pFileInfo info = new FileInfo;

         info->url = "http://192.168.34.72/pc_client/teacher-dev-mac/output/vipkid_mac_100/VIPKIDT-v1.7.5-mac-x64.dmg";

         info->fileMD5 = getFileMd5("./VIPKIDT-v-mac-x64.dmg");

         info->targetname = "VIPKIDT.dmg";

         info->savepath = "./";

         pthread_t pthid;

         pthread_create(&pthid, NULL,thread_function, (void*)info );

         int key ;

         while(1)

         {

             key = scanKeyboard();

             switch (key) {

                 case 115:

                     thread_pause();

                     break;

                 case 114:

                     thread_resume();

                     break;

                 case 112:

                     thread_resume();

                     break;

                 default:

                     break;

             }

             

         }

        return 0;

    }

  • 相关阅读:
    linux进程调度策略(SCHED_OTHER,SCHED_FIFO,SCHED_RR)
    10、单机运行环境搭建之 --Windows下mysqldump 备份与还原数据库
    5、单机运行环境搭建之 --CentOS6.4拷贝Tomcat7形成集群
    9、单机运行环境搭建之 --CentOS6.4下mysqldump 备份与还原数据库
    7、单机运行环境搭建之 --CentOS6.4安装Memcached
    13、单机运行环境搭建之 --Centos6.4下iptables保护主机安全
    12、单机运行环境搭建之 --Centos6.4下webbench压力测试
    6、单机运行环境搭建之 --CentOS6.4安装MySQL 5.6.10并修改MySQL的root用户密码
    4、单机运行环境搭建之 --CentOS6.5优化Tomcat7
    8、单机运行环境搭建之 --CentOS6.4安装vsftpd
  • 原文地址:https://www.cnblogs.com/lizhanzhe/p/10868082.html
Copyright © 2020-2023  润新知