DWORD WINAPI ThreadFunc(LPVOID threadData)
{
}
我正在将它们打包成结构体,并将它们作为参数传递到CreateThread方法中,并尝试通过将它们强制转换为与我的结构体相同类型的LPVOID来解包它们。
我不确定如何在获取它后将其转换为结构体,以便在方法本身中使用它,我尝试了各种组合(示例附加),但它无法编译。
结构体:
#define numThreads 1
struct Data
{
int threads;
int delay;
int messages;
};
方法调用:
HANDLE hThread;
DWORD threadId;
struct Data *tData;
tData->threads = numThreads;
tData->messages = 3;
tData->delay = 1000;
// Create child thread
hThread = CreateThread(
NULL, // lpThreadAttributes (default)
0, // dwStackSize (default)
ThreadFunc, // lpStartAddress
&tData, // lpParameter
0, // dwCreationFlags
&threadId // lpThreadId (returned by function)
);
My attempt:
DWORD WINAPI ThreadFunc(LPVOID threadData)
{
struct Data tData = (struct Data)threadData;
int msg;
for(msg = 0; msg<5; msg++)
{
printf("Message %d from child\n", msg);
}
return 0;
}
编译器错误:
错误 C2440: '类型转换' : 无法将 'LPVOID' 转换为 'Data'
正如您所看到的,我已经实现了一种方法来循环处理一些消息,我正在尝试使事情稍微更加高级并添加一些进一步的功能。