【源码】:放源码了,线程类!
经常看到有人找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;
}
...全文
相关推荐
WTL的线程类
展示一小组ATL / WTL类,这些类简化了线程的使用。
实用代码脚本易语言源码线程类
实用代码脚本易语言源码线程类
源码信息线程类
源码记录,线程优化类。
java源码war-udemy-java-warrior:Udemy课程“放码过来!作个Java实战派”源代码
java 源码 war “放码过来!作个Java实战派”课程源代码 版本一览 JDK 12.0.1,启用preview阶段功能 MAVEN 3.6.1 Intellij IDEA Ultimate 2019.1 参考资源 :star: Java Never Sleep :star: Java Never Sleep :star: Java Never Sleep License See the file for license rights and limitations (MIT).
vc++6.0制鞋放码软件源码
vc++6.0制鞋放码软件源码,某高校设计的图形图像底层架构,非常适合搞图形图像的同学学习使用


进程/线程/DLL
