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

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

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

2019-11-14 11:44:48
字體:
供稿:網(wǎng)友

簡(jiǎn)述

通過線程類來管理線程,實(shí)現(xiàn)業(yè)務(wù)邏輯與線程管理分離

源代碼

接口類 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)//進(jìn)入構(gòu)造函數(shù)之前 先初始化 成員變量 有一些成員變量 必須先初始化 比如常量什么的{}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)//如果沒運(yùn)行 { 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;//設(shè)置線程的運(yùn)行狀態(tài)為假 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;}

業(yè)務(wù)類 SFTimer.h

#ifndef SFTimer_H#define SFTimer_H#include<windows.h>#include"SFThread.h"#define WM_UPDATETIME WM_USER+101 //用戶消息,每秒發(fā)送一次。class SFTimer:public SFRunnable{ private: HWND hwnd; int Day; int Hour; int Minute; int Second; void init();//初始化天、小時(shí)、分鐘、秒全部為零 void CountDown();//倒計(jì)時(shí) void Time();//正計(jì)時(shí) 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--)//天循環(huán) { for(;this->Hour>=0;this->Hour--)//小時(shí)循環(huán) { for(; this->Minute >=0; this->Minute--)//分鐘循環(huán) { for(; this->Second >=0; this->Second--)//秒循環(huán) { Sleep(1000);//Sleep看清楚間 this->SendUploadTimeMessage();//發(fā)送消息 } this->Second = 59; } } } }void SFTimer::Time(){ this->init();//初始化各個(gè)參數(shù) while(this->Minute<5)//5分鐘計(jì)時(shí) { 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();}

測(cè)試代碼 testMain.cpp

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

實(shí)現(xiàn)效果

如下圖所示:實(shí)現(xiàn)2分鐘倒計(jì)時(shí),以及5分鐘正計(jì)時(shí)。 這里寫圖片描述


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 乌恰县| 乌拉特后旗| 政和县| 嘉义市| 海伦市| 确山县| 凤阳县| 张家口市| 辽宁省| 绩溪县| 定襄县| 乌拉特后旗| 当雄县| 修水县| 富蕴县| 东丽区| 长葛市| 永城市| 莆田市| 赞皇县| 虹口区| 西乌珠穆沁旗| 徐州市| 吴江市| 遵化市| 博湖县| 镇赉县| 黄山市| 阜宁县| 兴宁市| 周口市| 肃宁县| 祁连县| 义马市| 历史| 二连浩特市| 大新县| 镇江市| 扬中市| 河北区| 新郑市|