在Web上的示例中,对CreateThread的调用通常会传递指向结构体的指针LPVOID lpParameter
,并且您使用该指针访问结构本身。通过线程例程多个变量和堆栈大小
#include <Windows.h>
#include <stdio.h>
struct Point
{
float x,y,z ;
} ;
DWORD WINAPI threadStartPoint(LPVOID data)
{
Sleep(1000) ;
Point *p = (Point*)data ;
printf("%f %f %f\n", p->x, p->y, p->z) ;
puts("Thread job done") ;
return 0 ;
}
// From main
int main()
{
DWORD threadId ;
Point p ;
p.x=2, p.y=3, p.z=4 ;
HANDLE handle = CreateThread(0, 0,
threadStartPoint,
(LPVOID)&p,
0, // ?? I think I should be using this parameter</b>
&threadId
) ;
if(!handle)
{
// Thread creation failed
puts("start fail\n");
}
else
{
printf("started on threadid=%d\n", threadId) ;
}
WaitForSingleObject(handle, 2000) ; // wait up to 2000 ms for the other thread to complete before moving on
puts("main thread Exiting..") ;
//system("pause") ;
}
我发现这是一个有点不方便的,因为你必须确保结构存在,并确保当线程执行完它是正确销毁。
我想开始我的线程,但经过正常栈参数即自动变量,或者也许,struct
本身到线程启动例程:
DWORD threadStartPointFuncStyleIWant(Data d) ;
所以我的问题是真的:
- 对于螺纹起点(CreateThread的),是我们限制到功能与以下形式的原型:
DWORD validThreadFunc(LPVOID pParamStruct) ;
- 或者我们可以开始功能的线程像
DWORD threadFunc1(int p1, int p2) ; DWORD threadFunc2(Data d) ;