MFC编程AfxBeginThread与CreateThread使用对比
来源:互联网 发布:女生看见男生笑 知乎 编辑:程序博客网 时间:2024/06/05 21:52
MFC实际开发过程中,使用CreateThread创建的线程总是会出现莫名其妙的Bug。在参阅查找信息后,发现MFC编程,应该用AfxBeginThread来创建线程。
引语:
转自:http://blog.163.com/sky_sgx/blog/static/199439194201110944749818/
如果用MFC编程,不要用CreateThread,如果只是使用Runtime Library,用_BegingThread,总之,不要轻易使用CreateThread。这是因为在MFC和RTL中的函数有可能会用到些它们所封装的公用变量,也就是说AfxBeginThread和_BeginThread都有自己的启动代码是CreateThread所没有的。在用CreateThread所创建的线程中使用MFC的类和RTL函数就有可能出现问题。如果你是用汇编编写win32程序并且在线程函数中也不调用MFC和RTL的函数,那用CreateThread就没问题,或者你虽然是用C写线程函数,但你很小心没调用RTL函数也不会有问题。
CreateThread是由操作系统提供的接口,而AfxBeginThread和_BeginThread则是编译器对它的封装。
示例:
接下来,用具体示例来看两者间的不同。
创建支持MFC的Win32程序。
VS2005创建Win32步骤:
Win32->Win32 Console Application。
Application Settings设置(Application type: Console application,Add common header files for: MFC,Additional options: Precompiled header )。
ConsoleThreadTest.cpp
// ConsoleThreadTest.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "ConsoleThreadTest.h"#ifdef _DEBUG#define new DEBUG_NEW#endif// The one and only application objectCWinApp theApp;using namespace std;DWORD WINAPI Proc1(LPVOID){ CWinThread* pThread = AfxGetThread(); cout<<"CreateThread::AfxGetThread"; if (pThread != NULL) { cout<<" Valid"<<endl; } else { cout<<" NULL"<<endl; } return 1;}UINT Proc2(LPVOID){ CWinThread* pThread = AfxGetThread(); cout<<"AfxBeginThread::AfxGetThread"; if (pThread != NULL) { cout<<" Valid"<<endl; } else { cout<<" NULL"<<endl; } return 1;}int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]){ int nRetCode = 0; // initialize MFC and print and error on failure if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) { // TODO: change error code to suit your needs _tprintf(_T("Fatal Error: MFC initialization failed\n")); nRetCode = 1; } else { ::CreateThread(0,0,Proc1,NULL,0,0); Sleep(1000); AfxBeginThread(Proc2,NULL); Sleep(1000); } return nRetCode;}
输出结果:
CreateThread::AfxGetThread NULL
AfxBeginThread::AfxGetThread Valid
- MFC编程AfxBeginThread与CreateThread使用对比
- MFC编程AfxBeginThread与CreateThread使用区别
- CreateThread与AfxBeginThread区别
- MFC全局函数AfxBeginThread 及其与CreateThread的区别
- MFC全局函数AfxBeginThread 及其与CreateThread的区别
- AfxBeginThread与CreateThread的区别
- 比较CreateThread,_beginthread与AfxbeginThread
- CreateThread 与 AfxBeginThread的区别
- 比较CreateThread,_beginthread与AfxbeginThread
- AfxBeginThread()和CreateThread()使用心得
- CreateThread(Win32 API) ,Afxbeginthread(MFC),_beginthreadex
- 多线程 AfxBeginThread 与 CreateThread 的区别
- _beginthreadex与createthread和AfxBeginThread的区别
- _beginthreadex与createthread和AfxBeginThread的区别
- 多线程 AfxBeginThread 与 CreateThread 的区别
- 多线程 AfxBeginThread 与 CreateThread 的区别
- 多线程 AfxBeginThread 与 CreateThread 的区别
- _beginthreadex与createthread和AfxBeginThread的区别
- tomcat Java heap space 内存溢出
- SQL UNION 和 UNION ALL 操作符
- 2013年2月22日VC++学习笔记
- SSH Secure Shell Client显示乱码解决方法
- 匈牙利命名法和类型系统
- MFC编程AfxBeginThread与CreateThread使用对比
- Eclipse Console 加大显示的行数,禁止弹出
- sysctl 一个错误问题的解决
- jQuery验证控件jquery.validate.js使用说明+中文API
- CAS 资料
- 网购火车票反思:搜索该提供啥保障?
- 程序崩溃,但暂停的位置不在错误发生的行的解决方法
- Linux下的XAMPP基本配置技巧(设置虚拟主机、添加FTP账户等)
- 有关TCP和UDP 粘包 消息保护边界