无法理解创建线程时的此错误

时间:2010-06-21 18:13:04

标签: c++ windows multithreading winapi

     HANDLE  hThread;
     DWORD   dwThreadId;

         hThread = CreateThread( 
     NULL,                   // default security attributes
     0,                      // use default stack size  
     MyThreadFunction,       // thread function name
     0,                      // argument to thread function 
     0,                      // use default creation flags 
     &dwThreadId);           // returns the thread identifier  <--Debugger takes me to this line?

错误指定第3个参数,但是当我双击错误时,它会转到最后一个参数?
尝试运行msdn CreateThread示例http://msdn.microsoft.com/en-us/library/ms682453%28VS.85%29.aspx

error C2664: 'CreateThread' : cannot convert parameter 3 from 'void (void)' to 'unsigned long (__stdcall *)(void *)'
        None of the functions with this name in scope match the target type

4 个答案:

答案 0 :(得分:3)

调试器只是将你带到声明的末尾。

在任何情况下,您的函数签名都是错误的,需要匹配函数指针类型。对于CreateThread,它应该是:

DWORD WINAPI ThreadProc(LPVOID lpParameter);

答案 1 :(得分:2)

您的功能签名与预期签名不符。

您的MythreadFunction函数应返回ULONG。

类似的东西:

DWORD WINAPI MyThreadFunction(LPVOID lpParameter) {
}

答案 2 :(得分:1)

双击错误时,会显示错误发生位置的来源。由于函数调用表达式跨越多行,因此它将选择表达式的最后一行。

问题是MyThreadFunction没有正确的函数类型。 MyThreadFunction是一个不带参数的函数,不返回任何内容。您需要将指针传递给一个带有一个参数(void*)并返回unsigned long的函数。

答案 3 :(得分:1)

单击错误会转到最后一个参数,因为go-to-error函数只能通过语句,整个函数调用是一个语句。

基本上,您的问题是MyThreadFunction签名错误。它应该是unsigned long __stdcall MyThreadFunction(void*)(或其等价物),但您写了void MyThreadFunction(void)(或其等价物)。

相关问题