,线程传参有关问题
求助,线程传参问题
#include "stdafx.h"
#include <iostream>
#include <windows.h>
DWORD WINAPI ThreadProc(LPVOID lParam);
int _tmain(int argc, _TCHAR* argv[])
{
double *test = new double(32.3);
HANDLE hThread = CreateThread(NULL,0,ThreadProc,test,0,NULL);
CloseHandle(hThread);
delete test;
while(1);
return 0;
}
DWORD WINAPI ThreadProc(LPVOID lParam)
{
double* dd = (double*)lParam;
printf_s("%f\n",*dd);
return 0;
}
打印的时候,为什么得不到正确的值? 我该怎样做,才能得到正确的输出呢?
------解决方案--------------------
#include "stdafx.h"
#include <iostream>
#include <windows.h>
DWORD WINAPI ThreadProc(LPVOID lParam);
int _tmain(int argc, _TCHAR* argv[])
{
double *test = new double(32.3);
HANDLE hThread = CreateThread(NULL,0,ThreadProc,test,0,NULL);
CloseHandle(hThread);
delete test;
while(1);
return 0;
}
DWORD WINAPI ThreadProc(LPVOID lParam)
{
double* dd = (double*)lParam;
printf_s("%f\n",*dd);
return 0;
}
打印的时候,为什么得不到正确的值? 我该怎样做,才能得到正确的输出呢?
------解决方案--------------------
- C/C++ code
#include "stdafx.h"
#include <iostream>
#include <windows.h>
DWORD WINAPI ThreadProc(LPVOID lParam);
int _tmain(int argc, _TCHAR* argv[])
{
double *test = new double(32.3);
HANDLE hThread = CreateThread(NULL,0,ThreadProc,test,0,NULL);
CloseHandle(hThread);
while(1);
delete test;
return 0;
}
DWORD WINAPI ThreadProc(LPVOID lParam)
{
double *dd = (double*)lParam;
printf("%f\n",*dd);
return 0;
}