国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C++多線程編程(線程類)

2019-11-14 10:19:56
字體:
來源:轉載
供稿:網友

簡述

通過線程類來管理線程,實現業務邏輯與線程管理分離

源代碼

接口類 SFRunnable.h

class SFRunnable{ public: virtual ~SFRunnable() {}; virtual void Run() = 0;};

線程類 SFThread.h

#ifndef __SFTHREAD_H__#define __SFTHREAD_H__#include <string>#include <windows.h>#include <SFThread.cpp

#include "SFThread.h"SFThread::SFThread(void) : m_pRunnable(NULL),m_bRun(false)//進入構造函數之前 先初始化 成員變量 有一些成員變量 必須先初始化 比如常量什么的{}SFThread::~SFThread(void){}SFThread::SFThread(SFRunnable * pRunnable) : m_ThreadName(""),m_pRunnable(pRunnable),m_bRun(false){}SFThread::SFThread(const char * ThreadName, SFRunnable * pRunnable) : m_ThreadName(ThreadName),m_pRunnable(pRunnable),m_bRun(false){}SFThread::SFThread(std::string ThreadName, SFRunnable * pRunnable) : m_ThreadName(ThreadName),m_pRunnable(pRunnable),m_bRun(false){}bool SFThread::Start(bool bSuspend){ if(m_bRun) { return true; } if(bSuspend) { m_handle = (HANDLE)_beginthreadex(NULL, 0, StaticThreadFunc, this, CREATE_SUSPENDED, &m_ThreadID); } else { m_handle = (HANDLE)_beginthreadex(NULL, 0, StaticThreadFunc, this, 0, &m_ThreadID); } m_bRun = (NULL != m_handle); return m_bRun;}void SFThread::Run(){ if(!m_bRun)//如果沒運行 { return; } if(NULL != m_pRunnable)//如果句柄不為空 { m_pRunnable->Run(); } m_bRun = false;}void SFThread::Join(int timeout){ if(NULL == m_handle || !m_bRun) { return; } if(timeout <= 0) { timeout = INFINITE; } ::WaitForSingleObject(m_handle, timeout);}void SFThread::Resume(){ if(NULL == m_handle || !m_bRun) { return; } ::ResumeThread(m_handle);}void SFThread::Suspend(){ if(NULL == m_handle || !m_bRun) { return; } ::SuspendThread(m_handle);}bool SFThread::Terminate(unsigned long ExitCode){ if(NULL == m_handle || !m_bRun) { return true; } if(::TerminateThread(m_handle, ExitCode)) { ::CloseHandle(m_handle); m_bRun = false;//設置線程的運行狀態為假 return true; } return false;}unsigned int SFThread::GetThreadID(){ return m_ThreadID;}std::string SFThread::GetThreadName(){ return m_ThreadName;}void SFThread::SetThreadName(std::string ThreadName){ m_ThreadName = ThreadName;}void SFThread::SetThreadName(const char * ThreadName){ if(NULL == ThreadName) { m_ThreadName = ""; } else { m_ThreadName = ThreadName; }}unsigned int SFThread::StaticThreadFunc(void * arg){ SFThread * pThread = (SFThread *)arg; pThread->Run(); return 0;}

業務類 SFTimer.h

#ifndef SFTimer_H#define SFTimer_H#include<windows.h>#include"SFThread.h"#define WM_UPDATETIME WM_USER+101 //用戶消息,每秒發送一次。class SFTimer:public SFRunnable{ private: HWND hwnd; int Day; int Hour; int Minute; int Second; void init();//初始化天、小時、分鐘、秒全部為零 void CountDown();//倒計時 void Time();//正計時 public: SFTimer(); SFTimer(HWND hwnd); int getDay(); void setDay(int day); int getHour(); void setHour(int hour); int getMinute(); void setMinute(int minute); int getSecond(); void setSecond(int second); void Run(); void SendUploadTimeMessage();};#endif

SFTmier.cpp

#include "SFTimer.h"SFTimer::SFTimer(){ this->init();}SFTimer::SFTimer(HWND hwnd){ this->init(); this->hwnd = hwnd;}int SFTimer::getDay(){ return this->Day;}void SFTimer::setDay(int day){ this->Day = day;}int SFTimer::getHour(){ return this->Hour;}void SFTimer::setHour(int hour){ this->Hour = hour;}int SFTimer::getMinute(){ return this->Minute;}void SFTimer::setMinute(int minute){ this->Minute = minute;}int SFTimer::getSecond(){ return this->Second;}void SFTimer::setSecond(int second){ this->Second = second;}void SFTimer::SendUploadTimeMessage(){ PostMessage(this->hwnd,WM_UPDATETIME,this->Minute,this->Second);}void SFTimer::init(){ this->Day = 0; this->Hour = 0; this->Minute = 0; this->Second = 0;}void SFTimer::CountDown(){ for(;this->Day>=0;this->Day--)//天循環 { for(;this->Hour>=0;this->Hour--)//小時循環 { for(; this->Minute >=0; this->Minute--)//分鐘循環 { for(; this->Second >=0; this->Second--)//秒循環 { Sleep(1000);//Sleep看清楚間 this->SendUploadTimeMessage();//發送消息 } this->Second = 59; } } } }void SFTimer::Time(){ this->init();//初始化各個參數 while(this->Minute<5)//5分鐘計時 { this->Second+=1; if(this->Second > 60) { this->Second = 0; this->Minute += 1; if(this->Minute > 60) { this->Minute = 0; this->Hour += 1; if(this->Hour > 60) { this->Hour = 0; this->Day +=1; } } } this->SendUploadTimeMessage(); Sleep(1000); }}void SFTimer::Run(){ this->CountDown(); this->Time();}

測試代碼 testMain.cpp

#include <iostream>using namespace std;#include "SFTimer.h"#include "SFThread.h"int main(int argc, char *argv[]) { SFTimer* timer = new SFTimer();//具體業務類 timer->setDay(0);//設置天 timer->setHour(0);//設置小時 timer->setMinute(2);//設置分鐘 timer->setSecond(0);//設置秒 SFThread* thread = new SFThread(timer);//線程類 thread->Start();//啟動線程 while(1) { cout<<"計時開始:"<<timer->getMinute()<<"分"<<timer->getSecond()<<"秒"<<endl; Sleep(1000);//Sleep看清楚間 system("cls"); } getchar(); return 0; }

實現效果

如下圖所示:實現2分鐘倒計時,以及5分鐘正計時。 這里寫圖片描述


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 堆龙德庆县| 海南省| 绥江县| 郓城县| 喀什市| 蒙阴县| 宜章县| 江永县| 尤溪县| 综艺| 垦利县| 昆明市| 溆浦县| 雅安市| 泗水县| 县级市| 宁乡县| 南靖县| 都兰县| 宁波市| 来宾市| 托克逊县| 丹巴县| 德惠市| 武功县| 那坡县| 临沧市| 定日县| 尼玛县| 抚远县| 洞头县| 桐城市| 涞水县| 澄迈县| 新巴尔虎左旗| 余江县| 天柱县| 寿阳县| 南安市| 孟连| 隆尧县|