AfxBeginThread()和CreateThread()这两个函数有什么区别,分别用在什么场合?

xiaogang2000 2004-11-22 02:50:40
AfxBeginThread()和CreateThread()这两个函数有什么区别,分别用在什么场合?
谢谢。
...全文
355 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
Zark 2005-05-03
  • 打赏
  • 举报
回复
我看了msdn了,不过如果CreateThread()不带参数呢,他执行的是什么地方的程序?

答: 他什么也不执行,而你会得到"编译错误"!
蒋晟 2005-05-03
  • 打赏
  • 举报
回复
Visual C++ programmer's guide/Multithreading topics/Multithreading programing tips:
"If you have a multithreaded application that creates a thread in a way other than using a CWinThread object, you cannot access other MFC objects from that thread. In other words, if you want to access any MFC object from a secondary thread, you must create that thread with one of the methods described in the Multithreading: Creating User-Interface Threads or Multithreading: Creating Worker Threads articles. These methods are the only ones that allow the class library to initialize the internal variables necessary to handle multithreaded applications."

see also
http://www.microsoft.com/msj/0799/Win32/Win320799.aspx
IloveAzhu 2005-05-03
  • 打赏
  • 举报
回复
呵呵,建议大家看看候杰得《深入浅出MFC》里面有详细得介绍
并不是象大家说得那样没有区别得!
AFX。。得东西一般都会多做很多善后处理得!
随便哪个都是得!
wyfcat 2005-05-02
  • 打赏
  • 举报
回复
mark一下
an_bachelor 2004-11-23
  • 打赏
  • 举报
回复
AfxBeginThread()只能用於非MFC應用程序,CreateThread()到處都能用。
xiaogang2000 2004-11-23
  • 打赏
  • 举报
回复
我看了msdn了,不过如果CreateThread()不带参数呢,他执行的是什么地方的程序?
aben456 2004-11-23
  • 打赏
  • 举报
回复
基本没什么区别,封装与没被封装;
用法也是一样的
loki2k 2004-11-23
  • 打赏
  • 举报
回复
HANDLE CreateThread(
LPSECURITY_ATTRIBUTES lpThreadAttributes, // pointer to security attributes
DWORD dwStackSize, // initial thread stack size
LPTHREAD_START_ROUTINE lpStartAddress, // pointer to thread function
LPVOID lpParameter, // argument for new thread
DWORD dwCreationFlags, // creation flags
LPDWORD lpThreadId // pointer to receive thread ID
);
第三个参数你看到了吗?参看MSDN很明白的解析,
xiaogang2000 2004-11-23
  • 打赏
  • 举报
回复
xiaogang2000 2004-11-22
  • 打赏
  • 举报
回复
谢谢大家的回答。
我还有一个地方不太明白,比如有下面两个语句:
对于:AfxBeginThread(SendThreadProc,(LPVOID)this);
我知道新建的线程会一直执行函数SendThreadProc(LPVOID pParam);直到线程退出

但对于:CreateThread(),他执行的是什么地方的程序?
oyljerry 2004-11-22
  • 打赏
  • 举报
回复
一个是MFC的封装好的,一个是Windows API,
前者好用,后者使用麻烦些
danyueer 2004-11-22
  • 打赏
  • 举报
回复
AfxBeginThread是MFC类,派生自CCmdTarget,封装了线程的创建和一些操作,同时让被创建的线程能够响应Windows消息映射机制。AfxBeginThread只能在支持MFC的程序中使用。

CreateThread是Windows API,作用是创建一个线程并返回它的句柄。使用CreateThread需要Windows 平台SDK的支持,对MFC没有依赖。

在MFC程序中为了简化操作或让线程能够响应消息,可以使用AfxBeginThread;任何场合都可以使用CreateThread。
老夏Max 2004-11-22
  • 打赏
  • 举报
回复
没有什么区别!!参看定义:
HANDLE CreateThread(
LPSECURITY_ATTRIBUTES lpThreadAttributes, // pointer to security attributes
DWORD dwStackSize, // initial thread stack size
LPTHREAD_START_ROUTINE lpStartAddress, // pointer to thread function
LPVOID lpParameter, // argument for new thread
DWORD dwCreationFlags, // creation flags
LPDWORD lpThreadId // pointer to receive thread ID
);

AfxBeginThread
CWinThread* AfxBeginThread( AFX_THREADPROC pfnThreadProc, LPVOID pParam, int nPriority = THREAD_PRIORITY_NORMAL, UINT nStackSize = 0, DWORD dwCreateFlags = 0, LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL );

CWinThread* AfxBeginThread( CRuntimeClass* pThreadClass, int nPriority = THREAD_PRIORITY_NORMAL, UINT nStackSize = 0, DWORD dwCreateFlags = 0, LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL );

AfxBeginThread只是封装了CreateThread。

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧