我有一个问题困扰了我一段时间。我的项目中的每个子线程都在正确运行,并且除了以设置为 87 的 Last Error 开始线程之外,它应该做它应该做的事情。
根据 Win32 系统错误,87 表示无效参数。由于 LastError 是特定于线程的,并且从 ThreadProc 函数的第一行开始,它似乎已被设置,我唯一能推断的是 ThreadProc 函数本身在语法上是错误的(?)。
我的操作系统是 Windows 7 x64,编译器是 gcc 版本 4.6.2 我做了一个小示例程序,它在我的系统中启动子线程并设置错误 87。
#include <windows.h>
DWORD WINAPI THREAD_FUNCTION(LPVOID t)
{
printf("In the child thread: Last Error is %lu\n",GetLastError());
return 0;
}
typedef struct thread_data
{
//just an id for example's sake
uint32_t id;
}thread_data;
int main()
{
HANDLE thread;
thread_data d;
d.id = 1;
printf("Main thread start:Last error is %lu\n",GetLastError());
//create the thread
thread = CreateThread(NULL,0, (LPTHREAD_START_ROUTINE) THREAD_FUNCTION,(LPVOID)&d,0, NULL);
//wait for it
WaitForSingleObject(thread,INFINITE);
CloseHandle(thread);
printf("Main thread finish: Last error is %lu\n",GetLastError());
return 0;
}
这输出:
Main thread start:Last error is 0
In the thread: Last Error is 87
Main thread finish: Last error is 0
我认为这是我调用线程并将数据传递给它的方式的错误,但我无法通过阅读文档来推断此错误。有任何想法吗?