CWinThread* m_pThrd;
m_pThrd = AfxBeginThread(RUNTIME_CLASS(CTestThread));像这样的写法,创建界面线程,但是没有窗口,只是想使用它的消息队列。创建了线程后,该线程对象的生命周期是多少?对象的存储位置是在堆上还是栈上?如果是一个独立的线程空间,那么线程对象的数据成员应该是存储在线程对象自己的栈空间内,还是存储在创建该线程的创建者的栈空间内?Windows这样封装让我搞不明白了。高分急求!!!

解决方案 »

  1.   

    AfxBeginThread creates a new CWinThread object, calls its CreateThread function to start executing the thread, and returns a pointer to the thread. 
    To end the thread, call AfxEndThread from within the thread, or return from the controlling function of the worker thread.在堆上创建的,线程退出或调用AfxEndThread,m_pThrd就无效了。
    每个线程都有独立的栈
      

  2.   


    另外一个问题:InitInstance()这个函数是何时被调用的?是在构造函数之后,run函数之前调用?还是,在构造函数之前就调用了?
      

  3.   

    InitInstance()肯定是在run函数之前调用