首页 > 学院 > 开发设计 > 正文

C++多线程编程(线程类)

2019-11-14 11:44:48
字体:
来源:转载
供稿:网友

简述

通过线程类来管理线程,实现业务逻辑与线程管理分离

源代码

接口类 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 <PRocess.h>#include "SFRunnable.h"class SFThread : public SFRunnable{private: explicit SFThread(const SFThread & rhs);//explicitpublic: SFThread(); SFThread(SFRunnable * pRunnable); SFThread(const char * ThreadName, SFRunnable * pRunnable = NULL); SFThread(std::string ThreadName, SFRunnable * pRunnable = NULL); ~SFThread(void); /** 开始运行线程 @arg bSuspend 开始运行时是否挂起 **/ bool Start(bool bSuspend = false); /** 运行的线程函数,可以使用派生类重写此函数 **/ virtual void Run(); /** 当前执行此函数线程等待线程结束 @arg timeout 等待超时时间,如果为负数,等待无限时长 **/ void Join(int timeout = -1); /** 恢复挂起的线程 **/ void Resume(); /** 挂起线程 **/ void Suspend(); /** 终止线程的执行 **/ bool Terminate(unsigned long ExitCode); unsigned int GetThreadID(); std::string GetThreadName(); void SetThreadName(std::string ThreadName); void SetThreadName(const char * ThreadName);private: static unsigned int WINAPI StaticThreadFunc(void * arg);//线程处理函数private: HANDLE m_handle;//线程句柄 SFRunnable * const m_pRunnable;//执行逻辑的指针 unsigned int m_ThreadID;//线程ID std::string m_ThreadName;//线程name volatile bool m_bRun;//线程是否运行};#endif/*volatile:A volatile specifier is a hint to a compiler that an object may change its value in ways not specified by the language so that aggressive optimizations must be avoided.*/

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分钟正计时。 这里写图片描述


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表