AfxBeginThread函数初探

在进行多线程程序设计的时候,我们经常用到AfxBeginTh read函数来启动一条线程该函数使用起来非常的简单方便,其定 义如下
CWinThread* AfxBeginThr ead( AFX_THREADPROC pfnThrea dProc,//线程函数地址 LPVOID pParam ,//线程参数 int nPriority = THRE AD_PRIORITY_NORMAL,//线程优先级 U INT nStackSize = 0,//线程堆栈大小,默认 为1M DWORD dwCreateFlags = 0, // LPSECURITY_ATTRIBUTES lpS ecurityAttrs = NULL );

CWin Thread* AfxBeginThread( CRun timeClass* pThreadClass, int nPriority = THREAD_PRIORITY_N ORMAL, UINT nStackSize = 0, DWORD dwCreateFlags = 0, L PSECURITY_ATTRIBUTES lpSecurit yAttrs = NULL );

参数说明:pfnTh readProc:线程函数的地址,该参数不能设置为NULL, 线程函数必须定义成全局函数或者类的静态成员函数例如:UINT myThreadFunc(LPVOID lparam)或者 class A{public: static UINT __stdcall myThreadFunc(LP VOID lparam);
}之所以要定义成类的静态成员函数, 是因为类的静态成员函数不属于某个类对象,这样在调用函数的时候 就不用传递一个额外的this指针.
pThreadCl ass:指向从CWinThread派生的子类对象的RUNTI ME_CLASS
pParam:要传递给线程函数的参数
nPriority:要启动的线程的优先级,默认优先级 为THREAD_PRIORITY_NORMAL(普通优先级) ,关于线程 优先级的详细说明请参考Platform SDK SetThreadPriority函数说明
nStac kSize:新线程的堆栈大小,如果设置为0,则使用默认大小, 在应用程序中一般情况下线程的默认堆栈大小 为1M
dw CreateFlags:线程创建标志,该参数可以指定为下列标 志 CREATE_SUSPENDED:以挂起方式启动线程,如 果你在线程启动之前想初始化一些CWinThread类中的一些 成员变量 比如:m_bAutoDelete或者你的派生类中的 成员变量,当初始化完成之后,你可以使用CWinThread类 的ResumeThread 成员函数来恢复线程的运行 如果把 该标志设置为0,则表示立即启动线程lpSecurityAtt rs:指向安全描述符的指针,如果使用默认的安全级别只要讲该参 数设置为NULL就可以了!
上面就是AfxBeginT hread函数的简单说明,我们在使用的时候一般情况下只要指定 前两个参数,其他参数使用默认值就可以.嗯,的确,使用起来是很 简单,只要这个函数一被调用,就创建了一个线程.但是大家有没有 想过,AfxBeginThread函数究竟是如何启动的线程呢 ?它的内部是如何实现的呢?
下面我们就来看一下AfxB eginThread函数的内部实现
//启动worke r线程CWinThread* AFXAPI AfxBegin Thread(AFX_THREADPROC pfnThrea dProc, LPVOID pParam, int nPri ority, UINT nStackSize, DWORD dwCreateFlags, LPSECURITY_ATTR IBUTES lpSecurityAttrs){#ifnde f _MT pfnThreadProc;
pParam;
nPriori ty;
nStackSize;
dwCreateFlags;
lpSec urityAttrs;

return NULL;
#else ASSERT(pfn ThreadProc != NULL);

CWinThread* pThread = DEBUG _NEW CWinThread(pfnThreadProc, pParam);
ASSERT_VALID (pThread);

if (!pT hread->
CreateThread(dwCreat eFlags|CREATE_SUSPENDED, nStac kSize, lpSecu rityAttrs)) { pThread->
Delete();
return NULL;
} VERIFY(pThr ead->
SetThreadPriority(nPri ority));
if (!(dwCreat eFlags &
CREATE_SUSPENDED) ) VERIFY(pThr ead->
ResumeThread() != (DWO RD)-1);

return pTh read;
#endif //!_MT)}
//启动UI 线程CWinThread* AFXAPI AfxBeginT hread(CRuntimeClass* pThreadCl ass, int nPriority, UINT nStac kSize, DWORD dwCreateFlags, LP SECURITY_ATTRIBUTES lpSecurity Attrs){#ifndef _MT pThr eadClass;
nPriority;
nStackSize;
dwCrea teFlags;
lpSecurityAttr s;

return NULL;
#els e ASSERT(pThreadClass ! = NULL);
ASSERT(pThread Class->
IsDerivedFrom(RUNTIM E_CLASS(CWinThread)));

CWinThread* pThread = (CWi nThread*)pThreadClass->
Crea teObject();
if (pThread == NULL) AfxTh rowMemoryException();
A SSERT_VALID(pThread);

pThread->
m_pThreadParams = NULL;
if (!pThread-& gt;
CreateThread(dwCreateFlags| CREATE_SUSPENDED, nStackSize, lpSecurityAttrs )) { pTh read->
Delete();
return NULL;
} VERIFY(pThread->
SetThrea dPriority(nPriority));
if (!(dwCreateFlags &
CREA TE_SUSPENDED)) VERIFY(pThread->
ResumeThrea d() != (DWORD)-1);

return pThread;
#endif //!_MT}< br>从上面的代码中可以看出AfxBeginThread所做 的事情主要有以下几点:
1.在heap中配置一个新的C WinThread对象(worker线程)代码如:CWinT hread* pThread = DEBUG_NEW CWi nThread(pfnThreadProc, pParam) ;
调用CRuntimeClass结构中的CreateObje ct函数创建CWinThread对象CWinThread* pThread = (CWinThread*)pThread Class->
CreateObject();
CRunt imeClass以及MFC相关类的内部实现,详情请参考《深入 浅出MFC》侯捷著
2.调用CWinThread::C reateThread()并设定属性,使线程以挂起状态产生p Thread->
CreateThread(dwCrea teFlags|CREATE_SUSPENDED, nSta ckSize,lpSecurityAttrs);

3. 设定线程的优先权pThread->
SetThreadP riority(nPriority);

4.调用CWi nThread::ResumeThreadpThread-& gt;
ResumeThread();
通过上面的说明,我想大家 对该函数到底在内部都做了什么,应该有一个初步的了解了!对于V C老手来说,这篇文章可能并没有什么可读之处,但是对于初学者来 说,还是有一定的价值的!总之,希望这篇文章能给各位一点点的帮 助!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值