生活随笔
收集整理的這篇文章主要介紹了
简单的C++线程类实现, windows平台
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一個(gè)抽象的線程基類, 再來(lái)個(gè)具體的線程類并實(shí)現(xiàn)相關(guān)接口,再寫(xiě)個(gè)主函數(shù)來(lái)調(diào)用下。上代碼:
Thread.h
/*Windows平臺(tái)線程類實(shí)現(xiàn)開(kāi)發(fā)環(huán)境: Win7_x64 + VC2012
*/
#ifndef __THREAD_H__
#define __THREAD_H__#pragma once#include <string>
#include <windows.h>/*1. 線程基類, 要?jiǎng)?chuàng)建新的線程類, 只需要繼承此類并實(shí)現(xiàn)相關(guān)接口2. 要開(kāi)啟線程并運(yùn)行只需要調(diào)用Start()函數(shù)3. 未完善地方: 應(yīng)該寫(xiě)個(gè)虛函數(shù)Stop(), 當(dāng)線程過(guò)程在運(yùn)行時(shí)可以設(shè)置下運(yùn)行標(biāo)志變量讓線程退出循環(huán)過(guò)程, 再作些清理工作, 避免暴力終止線程。
*/
class CThread // 抽象的線程基類
{
public:CThread(const std::string threadName = "noNamed");virtual ~CThread();virtual void Run() = 0; // 線程執(zhí)行過(guò)程virtual bool Start(bool bSuspended/* = false*/);void Join(int timeout = -1); // 等待超時(shí)時(shí)間為負(fù)時(shí), 表示無(wú)限等待void Resume(); // 恢復(fù)掛起的線程void Suspend(); // 掛起線程bool Terminate(unsigned long exitCode); // 結(jié)束線程unsigned int GetThreadID(); // 獲取線程IDstd::string GetThreadName();void SetThreadName(std::string threadName);private:bool CreateThread(bool bSuspended = false);// 開(kāi)始運(yùn)行線程static unsigned int WINAPI StaticThreadFunc(void* arg); // 線程函數(shù)protected:HANDLE m_handle;std::string m_threadName;unsigned int m_threadID;volatile bool m_bRun; // 表明線程是否已成功創(chuàng)建(實(shí)際上與m_handle含義相同了)
};#endif
Thread.cpp:
#include <iostream>
#include <process.h>
#include "Thread.h"CThread::CThread(const std::string threadName): m_threadName(threadName), m_threadID(0), m_bRun(false)
{
}CThread::~CThread()
{printf("~CThread()\n");
}bool CThread::Start(bool bSuspend/* = false*/) // 創(chuàng)建線程并運(yùn)行(默認(rèn))或掛起
{m_bRun = CreateThread(bSuspend);return m_bRun;
}bool CThread::CreateThread(bool bSuspend/* = false*/) // 創(chuàng)建線程并運(yùn)行(默認(rèn))或掛起
{if(!m_bRun){if(bSuspend)m_handle = (HANDLE)_beginthreadex(NULL, 0, StaticThreadFunc, this, CREATE_SUSPENDED, &m_threadID);elsem_handle = (HANDLE)_beginthreadex(NULL, 0, StaticThreadFunc, this, 0, &m_threadID);m_bRun = (NULL != m_handle);}return m_bRun;
}void CThread::Join(int timeout/* = -1*/) // 等待超時(shí)時(shí)間(毫秒)為負(fù)時(shí), 表示無(wú)限等待
{if(m_handle && m_bRun){if(timeout < 0)timeout = INFINITE;::WaitForSingleObject(m_handle, timeout);}
}void CThread::Resume() // 恢復(fù)掛起的線程
{if(m_handle && m_bRun)::ResumeThread(m_handle);
}void CThread::Suspend() // 掛起線程
{if(m_handle && m_bRun)::SuspendThread(m_handle);
}bool CThread::Terminate(unsigned long exitCode) // 結(jié)束線程
{if(m_handle && m_bRun){if(::TerminateThread(m_handle, exitCode)){::CloseHandle(m_handle);m_handle = NULL;m_bRun = false;return true;}}return false;
}unsigned int CThread::GetThreadID()
{return m_threadID;
}std::string CThread::GetThreadName()
{return m_threadName;
}void CThread::SetThreadName(std::string threadName)
{m_threadName = threadName;
}unsigned int CThread::StaticThreadFunc(void* arg) // 線程函數(shù)
{CThread* pThread = (CThread*)arg; // 取得線程類指針pThread->Run(); // 執(zhí)行線程過(guò)程函數(shù)return 0;
}
Thread1.h
#ifndef __THREAD1_H__
#define __THREAD1_H__#pragma once#include "Thread.h"/*1. 要?jiǎng)?chuàng)建一個(gè)新線程類時(shí)只需要繼承CThread, 然后在Run()中實(shí)現(xiàn)自己的線程過(guò)程(Run())
*/
class CThread1: public CThread // 線程類1
{
public:CThread1(const std::string threadName = "noNamed");virtual ~CThread1(void);bool Start(bool bSuspended/* = false*/);virtual void Run();
};#endif
Thread1.cpp
#include <iostream>
#include "Thread1.h"CThread1::CThread1(const std::string threadName): CThread(threadName)
{}CThread1::~CThread1()
{printf("~CThread1()\n");
}bool CThread1::Start(bool bSuspended/* = false*/)
{// todo: 此處可添加一些初始化代碼return CThread::Start(bSuspended);
}void CThread1::Run()
{int cnt = 0;while(cnt <= 10){std::cout << "Hello " << m_threadName << "::Run(): " << cnt++ << std::endl;Sleep(200);}
}
main.cpp
#define _CRT_SECURE_NO_WARNINGS#include <iostream>
#include "Thread1.h"#define N 15int main(int argc, char* argv[])
{char buf[20] = {0};CThread* t[N] = {NULL};for(int i = 0; i < N; i++){sprintf(buf, "Thread%d", i+1);t[i] = new CThread1(buf);t[i]->Start(true);std::cout << t[i]->GetThreadName() << ": " << t[i]->GetThreadID() << std::endl;t[i]->Resume();}for(int i = 0; i < N; i++)t[i]->Join();return 0;
}
?
?
總結(jié)
以上是生活随笔為你收集整理的简单的C++线程类实现, windows平台的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。