生活随笔
收集整理的這篇文章主要介紹了
简单的C++线程类实现, windows平台
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一個抽象的線程基類, 再來個具體的線程類并實現(xiàn)相關(guān)接口,再寫個主函數(shù)來調(diào)用下。上代碼:
Thread.h
/*Windows平臺線程類實現(xiàn)開發(fā)環(huán)境: Win7_x64 + VC2012
*/
#ifndef __THREAD_H__
#define __THREAD_H__#pragma once#include <string>
#include <windows.h>/*1. 線程基類, 要創(chuàng)建新的線程類, 只需要繼承此類并實現(xiàn)相關(guān)接口2. 要開啟線程并運行只需要調(diào)用Start()函數(shù)3. 未完善地方: 應(yīng)該寫個虛函數(shù)Stop(), 當(dāng)線程過程在運行時可以設(shè)置下運行標(biāo)志變量讓線程退出循環(huán)過程, 再作些清理工作, 避免暴力終止線程。
*/
class CThread // 抽象的線程基類
{
public:CThread(const std::string threadName = "noNamed");virtual ~CThread();virtual void Run() = 0; // 線程執(zhí)行過程virtual bool Start(bool bSuspended/* = false*/);void Join(int timeout = -1); // 等待超時時間為負(fù)時, 表示無限等待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);// 開始運行線程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)建(實際上與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)建線程并運行(默認(rèn))或掛起
{m_bRun = CreateThread(bSuspend);return m_bRun;
}bool CThread::CreateThread(bool bSuspend/* = false*/) // 創(chuàng)建線程并運行(默認(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*/) // 等待超時時間(毫秒)為負(fù)時, 表示無限等待
{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í)行線程過程函數(shù)return 0;
}
Thread1.h
#ifndef __THREAD1_H__
#define __THREAD1_H__#pragma once#include "Thread.h"/*1. 要創(chuàng)建一個新線程類時只需要繼承CThread, 然后在Run()中實現(xiàn)自己的線程過程(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平台的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。