我有一个问题困扰了我一段时间。在我的项目中的每个子线程运行正常,并做什么是应该在87Winapi Error 87开始线程问题
87开头的最后错误设置线程的例外,做根据的Win32系统错误表示无效参数。由于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
我想这是我所说的线程和传递数据的方式错误,但我不能阅读文档推断此错误。有任何想法吗?
你应该在WaitForSingleObject之后调用'CloseHandle(thread);'。 – Lundin
我知道,这只是我用来演示我的问题的一个小例子程序。为了清楚起见,将进行编辑。谢谢 – Lefteris