zoukankan      html  css  js  c++  java
  • 线程启动、暂停与挂起

    ////

    ////  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;

    }

  • 相关阅读:
    Java单例模式:为什么我强烈推荐你用枚举来实现单例模式
    为什么阿里Java规约要求谨慎修改serialVersionUID字段
    使用MyCat实现MySQL读写分离
    你知道HTTP协议的ETag是干什么的吗?
    在centos7中安装MySQL5.7
    MySQL实现主从复制功能
    Leetcode题目169.求众数(简单)
    Leetcode题目160.相交链表(简单)
    Leetcode题目155.最小栈(简单)
    Leetcode题目152.乘积最大子序列(动态规划-中等)
  • 原文地址:https://www.cnblogs.com/lizhanzhe/p/10868082.html
Copyright © 2011-2022 走看看