【源码】:放源码了,线程类!

djfu djfu 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
经常看到有人找CThread类源码,自己修改了一个。


// HEADER FILE : thread.h
// Thread.h: interface for the CThread class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_THREAD_H__07971CDB_D943_4826_903B_1BA1FF7634A6__INCLUDED_)
#define AFX_THREAD_H__07971CDB_D943_4826_903B_1BA1FF7634A6__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <afxmt.h>
#include <windows.h>

typedef unsigned (WINAPI* THREAD_FUNC)(void *ThreadParam);

class CThread;
typedef struct tagThreadParam
{
CThread *pThread;
void *pParam;
} tThreadParam ;

class CThread
{
public:
CThread(THREAD_FUNC ThreadFunc, void *ThreadParam = NULL, bool bCreateSuspended = false, SECURITY_ATTRIBUTES* pSecurityAttributes = NULL, const char* ThreadName = NULL);
virtual ~CThread() { stop(100); }

int Start();
int Suspend();
int Resume();
void stop(unsigned long dwWait = INFINITE);
int Run();

HANDLE GetThreadHandle() const { return m_hThreadHandle; }
long GetThreadID() const { return m_nThreadID; }
operator HANDLE() const { return m_hThreadHandle; };
const char* GetThreadName() const { return (const char *)m_szThreadName; }

bool IsInvalid() const { return m_hThreadHandle <= 0 ? true : false; };
bool IsSuspended() const { return m_bSuspend; };
bool IsStopped() const { return m_bStopFlag; };
bool IsRunning();

private:
CThread(const CThread& ) {}
CThread& operator=( const CThread& ) {}

private:
bool m_bSuspend;
HANDLE m_hThreadHandle;
unsigned int m_nThreadID;
char m_szThreadName[512];
bool m_bStopFlag;
SECURITY_ATTRIBUTES*p_Attr;
THREAD_FUNC m_ThreadFunc;
void *m_pInputParam;
};

static unsigned WINAPI thread_function(void* pArg)
{
return ((CThread*)pArg)->Run();
}

#endif // !defined(AFX_THREAD_H__07971CDB_D943_4826_903B_1BA1FF7634A6__INCLUDED_)






// CPP FILE: thread.cpp
// Thread.cpp: implementation of the CThread class.
//
//////////////////////////////////////////////////////////////////////

#include "StdAfx.h"
#include "Thread.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////


#include "thread.h"
#include <process.h>


CThread::CThread(THREAD_FUNC ThreadFunc, void *ThreadParam, bool bCreateSuspended, SECURITY_ATTRIBUTES* pSecurityAttributes, const char* ThreadName):
p_Attr(pSecurityAttributes), m_hThreadHandle(0)
{
m_bStopFlag = false;
m_bSuspend = bCreateSuspended;
m_nThreadID = 0;
memset(m_szThreadName, 0, sizeof(m_szThreadName));
strncpy(m_szThreadName, ThreadName, min(strlen(ThreadName), sizeof(m_szThreadName) - 1));
m_ThreadFunc = ThreadFunc;
m_pInputParam = ThreadParam;
}

int CThread::Start()
{
tThreadParam param;
param.pParam = m_pInputParam;
param.pThread = this;
BOOL bRet = _beginthreadex(
p_Attr,
0,
m_ThreadFunc,//thread_function,
¶m,//this,
(m_bSuspend ? CREATE_SUSPENDED : 0 ),
&m_nThreadID);

m_hThreadHandle = (HANDLE)bRet;
m_ThreadFunc = thread_function;

return bRet;
}

// simulate to realize it
int CThread::Run()
{
while(!IsStopped())
{
int sum = 0;
for(int i = 0; i < 100; i ++)
{
sum += i;
}
printf("%d\n", sum);
Sleep(100);
}

_endthreadex(EXIT_SUCCESS);

return 0;
}

void CThread::stop(unsigned long dwWait)
{
if(IsInvalid())
return;

::WaitForSingleObject(m_hThreadHandle, dwWait);
m_bStopFlag = true;
}


int CThread::Suspend()
{
if(IsInvalid())
{
return -2;
}

if(::SuspendThread(m_hThreadHandle) == -1)
{
return -1;
}

m_bSuspend = true;
return 0;
}

int CThread::Resume()
{
if(IsInvalid())
{
return -2;
}

if(::ResumeThread(m_hThreadHandle) == -1)
{
return -1;
}

m_bSuspend = false;
return 0;
}


bool CThread::IsRunning()
{
if(IsInvalid())
return false;

if(m_bSuspend)
return false;

if(::WaitForSingleObject(m_hThreadHandle, 0) == WAIT_TIMEOUT)
return true;
else
return false;
}


...全文
评论 20
收藏 已收藏
分享
返回顶部
djfu
进程/线程/DLL 15,465 成员 49,169 内容