多线程下载开发

来源:互联网 发布:软件质量管理 编辑:程序博客网 时间:2024/06/05 17:19
// 多线程下载 (1.0)#include <iostream>#include <string>#include <sstream> //stringstream#include <thread>#include <vector>#include <mutex>#include <chrono> // 下载类class Download{public:    Download(int id, std::string name)        : mId(id), mName(name), mFinish(false)    {    }    // 下载函数    void operator()()  //   void operator()() const    {        while (!mFinish) {                std::lock_guard<std::mutex> lock(mMutex);                std::cout << "线程" << mId << " 正在下载... " << std::endl;                // 假如3s下载完成                std::this_thread::sleep_for(std::chrono::seconds(3));                std::cout << "线程" << mId << " 下载完成... " << std::endl;                mFinish = true;        }    }private:    int mId;    std::string mName;    bool mFinish;    static std::mutex mMutex;};std::mutex Download::mMutex;int main(){    int n = 10;    std::vector<std::thread> thread;    for (int i = 0; i < n; ++i) {        std::stringstream ss;        ss << "名称 " << i << " 线程";        thread.push_back(std::thread{ Download(i,ss.str()) });    }    for (auto& t : thread) {        t.join();    }    return 0;}
// 多线程下载 (2.0)#include <iostream>#include <string>#include <sstream> //stringstream#include <thread>#include <vector>#include <mutex>#include <chrono> // 下载类class   CDownload{public:    CDownload() = delete;    CDownload(int id, std::string name)        : mId(id), mName(name)    {        mIsPause = false;        mIsFinish = false;    }    CDownload(const CDownload& src)    {        //std::cout << "copy..." << std::endl;        mId=src.mId;        mName=src.mName;        mIsPause = false;        mIsFinish = false;    }    ~CDownload() {        if (mThread.joinable())            mThread.join();    }    // 线程启动    void Run()    {        mThread = std::thread{ &CDownload::ProcessEntries,this };    }    // 下载函数    void ProcessEntries()    {        while (!mIsFinish) {            //std::lock_guard<std::mutex> lock(mMutex);            if (mIsPause) {                std::this_thread::sleep_for(std::chrono::seconds(2));                std::cout << "-";            }            else {                std::this_thread::sleep_for(std::chrono::seconds(2));                std::cout << ".";                // 假如3s下载完成                std::this_thread::sleep_for(std::chrono::seconds(3));                std::cout << mId << "+";                mIsFinish = true;            }        }    }    // 暂停    void Pause() { mIsPause = true; }    // 唤醒    void Resume() { mIsPause = false; }private:    int mId;    std::string mName;    bool mIsPause;    bool mIsFinish;    std::thread mThread;    static std::mutex mMutex;};std::mutex CDownload::mMutex;// 下载管理类int main(){    int n = 10;    std::vector<CDownload> thread;    for (int i = 0; i < n; ++i) {        std::stringstream ss;        ss << " 线程" << i << "名称";        //CDownload Download(i, ss.str());        thread.push_back(std::move(CDownload{ i, ss.str() }));        // 这里有个特别值得注意的地方:此时vector内存不够 需要扩大内存,重新分配内存 这时再调用拷贝构造函数将Download拷贝到新的内存    }    // 启动线程    for (auto& t : thread) {        t.Run();    }    // 暂定指定线程    std::this_thread::sleep_for(std::chrono::seconds(4));    thread[1].Pause();    // 唤醒指定线程    std::this_thread::sleep_for(std::chrono::seconds(4));    thread[1].Resume();    // 全部暂停    std::this_thread::sleep_for(std::chrono::seconds(4));    for (auto& t : thread) {        t.Pause();    }    // 全部开始    std::this_thread::sleep_for(std::chrono::seconds(4));    for (auto& t : thread) {        t.Resume();    }    return 0;}
// 多线程下载 (3.0)#include <iostream>#include <string>#include <sstream> //stringstream#include <thread>#include <vector>#include <mutex>#include <chrono>#include <exception>// 下载类class CDownload{public:    CDownload() = delete;    CDownload(int id, std::string& name)        : mId(id), mName(name)    {        mIsPause = false;        mIsFinish = false;    }    CDownload(const CDownload& src)    {        //std::cout << "copy..." << std::endl;        mId=src.mId;        mName=src.mName;        mIsPause = false;        mIsFinish = false;    }    ~CDownload() {        if (mThread.joinable())            mThread.join();    }    // 线程启动    void Run()    {        mThread = std::thread{ &CDownload::ProcessEntries,this };    }    // 下载函数    void ProcessEntries()    {        while (!mIsFinish) {            //std::lock_guard<std::mutex> lock(mMutex);            if (mIsPause) {                std::this_thread::sleep_for(std::chrono::seconds(2));                std::cout << "-";            }            else {                std::this_thread::sleep_for(std::chrono::seconds(2));                std::cout << ".";                // 假如3s下载完成                std::this_thread::sleep_for(std::chrono::seconds(3));                std::cout << mId << "+";                mIsFinish = true;            }        }    }    // 暂停    void Pause() { mIsPause = true; }    // 唤醒    void Resume() { mIsPause = false; }private:    int mId;    std::string mName;    bool mIsPause;    bool mIsFinish;    std::thread mThread;    static std::mutex mMutex;};std::mutex CDownload::mMutex;// 下载管理类class CDownloadMgr{public:    CDownloadMgr() {}    ~CDownloadMgr() {}    // 创建新下载    void CreateDownload(CDownload& NewDownload)    {        mVthread.push_back(NewDownload);        (mVthread.end() - 1)->Run();    }    // 创建新下载    void CreateDownload(int id, std::string& name)    {        CDownload Download(id, name);        mVthread.push_back(Download);        (mVthread.end()-1)->Run();    }    // 暂定指定线程    void PauseOne(int nIndex)    {        if (nIndex >= mVthread.size())            throw std::runtime_error("暂定指定线程出错");        mVthread.at(nIndex).Pause();    }    // 唤醒指定线程    void ResumeOne(int nIndex)    {        if (nIndex >= mVthread.size())            throw std::runtime_error("唤醒指定线程出错");        mVthread.at(nIndex).Resume();    }    // 全部暂停    void PauseALL()    {        for (auto& t : mVthread) {            t.Pause();        }    }    // 全部开始    void StartALL()    {        for (auto& t : mVthread) {            t.Resume();        }    }private:    std::vector<CDownload> mVthread;};int main(){    CDownloadMgr DownloadMgr;    int n = 10;    for (int i = 0; i < n; ++i) {        std::stringstream ss;        ss << " 线程" << i << "名称";        DownloadMgr.CreateDownload(i, ss.str());    }    return 0;}
0 0
原创粉丝点击