在类的构造函数中开启线程,该怎么解决

在类的构造函数中开启线程
伪代码:
 CA()
 {
  CreateThread( 
NULL, // default security attributes
0, // use default stack size  
DoXlanMsg, // thread function 
this, // argument to thread function 
0, // use default creation flags 
&dwThreadId[i]); // returns the thread identifier 
  }

  *** DoXlanMsg(*** lp)
  {
  CA* app = (CA*)lp;
  if (NULL == app)
{
return 0;
}
  }

为什么release版本会进入 return 0阿?在debug版本中不进.
构造函数中应该就已经开始实例化对象了阿。

 

------解决方案--------------------
我觉得release也应该不会参数无效吧。
你可以试试_beginthreadex,如果是MFC,试试AfxBeginThread
------解决方案--------------------
我试了下,Release不会进lz说的code啊

#include "iostream"
#include <windows.h>
using namespace std;


DWORD __stdcall DoXlanMsg(void* pv)
{
if (NULL == pv)
{
cout<<"pv == NULL"<<endl;
return 0;
}
int i=0;
while (1)
{
Sleep(1000);
cout<<"hello "<<i++<<endl;
}
return 0;
}

class CA
{
public:
CA()
{
/*m_hThread=*/CreateThread( 
NULL, // default security attributes
0, // use default stack size
DoXlanMsg, // thread function 
this, // argument to thread function 
0, // use default creation flags 
NULL); // returns the thread identifier
}
~CA()
{
if (m_hThread)
{
//TerminateThread(m_hThread,0);
}
}

HANDLE GetThrd(){return m_hThread;}
private:
HANDLE m_hThread;

};

int main()
{
cout<<"hello"<<endl;
CA a;
//WaitForSingleObject(a.GetThrd(),INFINITE);
Sleep(50000);
return 0;
}
------解决方案--------------------
lz可以在进入CreateThread函数之前打印this的值看看
------解决方案--------------------
这个由OS的线程调度机制来决定,可能线程是在初始化对象后才启动,也有可能线程是在对象初始化前就已经启动了……
这代码写的……
------解决方案--------------------
探讨

这个由OS的线程调度机制来决定,可能线程是在初始化对象后才启动,也有可能线程是在对象初始化前就已经启动了……
这代码写的……

------解决方案--------------------
探讨
引用:

这个由OS的线程调度机制来决定,可能线程是在初始化对象后才启动,也有可能线程是在对象初始化前就已经启动了……
这代码写的……

只要进入构造函数,就已经为对象分配内存了,此时this指针怎么着也不会为NULL啊