CreateThread创建线程时为什么报故呢?

迷途的书童 2008-11-12 02:50:01
CreateThread创建线程时为什么报:error C2664: 'CreateThread' : cannot convert parameter 3 from 'int (void *)' to 'unsigned long (__stdcall *)(void *)'
None of the functions with this name in scope match the target type


#include <windows.h>
#include <iostream>
using namespace std;
int pro()
{
cout<<"Hello!"<<endl;
return 0;
}
int main()
{
HANDLE handle;
int id;
handle=::CreateThread(NULL,0,pro,NULL,0,&id);
::WaitForSingleObject(handle,INFINITE);
::CloseHandle(handle);
return 0;

}
...全文
396 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
wryse 2008-11-12
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 GUANYIJUN123 的回复:]
[Quote=引用 9 楼 ztz0223 的回复:]
修改如下:
谢谢!这是正确答案!
原来是这第6个参数错吴造成,感觉很奇怪为什么报第3个参数有故障呢?
C/C++ code#include <windows.h>
#include <iostream>
using namespace std;
DWORD WINAPI pro(LPVOID)
{
cout<<"Hello!"<<endl;
return 0;
}
int main()
{
HANDLE handle;
int id;
handle=::CreateThread(NULL,0,pro,NULL,0,(LPDWORD)&id);//原来…
[/Quote]

呃,因为第三个参数在函数定义那边的确也有错……比较一下ztz0223关于pro函数的定义和LZ自己的函数定义应该就能知道了吧……
迷途的书童 2008-11-12
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 ztz0223 的回复:]
修改如下:
谢谢!这是正确答案!
原来是这第6个参数错吴造成,感觉很奇怪为什么报第3个参数有故障呢?
#include <windows.h>
#include <iostream>
using namespace std;
DWORD WINAPI pro(LPVOID)
{
cout<<"Hello!"<<endl;
return 0;
}
int main()
{
HANDLE handle;
int id;
handle=::CreateThread(NULL,0,pro,NULL,0,(LPDWORD)&id);//原来是这第6个参数错吴造成,感觉很奇怪为什么报第3个参数有故障呢?
::WaitForSingleObject(handle,INFINITE);
::CloseHandle(handle);
return 0;

}
[/Quote]
迷途的书童 2008-11-12
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 la_feng 的回复:]
#include <windows.h> 
#include <iostream>
using namespace std;
int pro()
{
cout < <"Hello!" < <endl;
return 0;
}
int main()
{
HANDLE handle;
DWORD id;
handle=::CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)pro,NULL,0,&id); //就是这里没法转换成(LPTHREAD_START_ROUTINE)造成的所
//以再加也报一样的故障
::WaitForSingleObject(handle,INFINITE);
::CloseHandle(handle);
return 0;

}
[/Quote]
wryse 2008-11-12
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 GUANYIJUN123 的回复:]
返回值是要的!因为要有返回值,WaitForSingleObject()函数才知道线程运行完毕!
[/Quote]
那就是这样
DWORD WINAPI pro(void* pParam)
{
cout<<"Hello!"<<endl;
return 0;
}
好久没用记不得了……俺果然老了……(叹)
迷途的书童 2008-11-12
  • 打赏
  • 举报
回复
返回值是要的!因为要有返回值,WaitForSingleObject()函数才知道线程运行完毕!
la_feng 2008-11-12
  • 打赏
  • 举报
回复
#include <windows.h>
#include <iostream>
using namespace std;
int pro()
{
cout<<"Hello!"<<endl;
return 0;
}
int main()
{
HANDLE handle;
DWORD id;
handle=::CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)pro,NULL,0,&id);
::WaitForSingleObject(handle,INFINITE);
::CloseHandle(handle);
return 0;

}
就呆在云上 2008-11-12
  • 打赏
  • 举报
回复
修改如下:
#include <windows.h>
#include <iostream>
using namespace std;
DWORD WINAPI pro(LPVOID)
{
cout<<"Hello!"<<endl;
return 0;
}
int main()
{
HANDLE handle;
int id;
handle=::CreateThread(NULL,0,pro,NULL,0,(LPDWORD)&id);
::WaitForSingleObject(handle,INFINITE);
::CloseHandle(handle);
return 0;

}
迷途的书童 2008-11-12
  • 打赏
  • 举报
回复

改成:unsigned long pro(PVOID pParam)
还是报这个
error C2664: 'CreateThread' : cannot convert parameter 3 from 'unsigned long (void *)' to 'unsigned long (__stdcall *)(void *)'
None of the functions with this name in scope match the target type

unsigned long pro(PVOID pParam)
{
cout<<"Hello!"<<endl;
return 0;
}



int main()
{
HANDLE handle;
int id;
handle=::CreateThread(NULL,0,pro,NULL,0,&id);
::WaitForSingleObject(handle,INFINITE);
::CloseHandle(handle);
return 0;

}
wryse 2008-11-12
  • 打赏
  • 举报
回复

DWORD WINAPI pro(void* pParam)
{
cout<<"Hello!"<<endl;
//return 0;
//好像是不要返回值的,记不清了,LZ自己试一下吧……
}

这样应该就行了吧……
healer_kx 2008-11-12
  • 打赏
  • 举报
回复
类型不匹配而已,函数指针的类型,你再研究一下了,主要那个__stdcall了。
wryse 2008-11-12
  • 打赏
  • 举报
回复
CreateThread的第三个参数是指向线程函数的指标。函数名称没有限制,但是必须以下列形式声明:
DWORD WINAPI ThreadProc (PVOID pParam)
因此LZ直接写int pro()肯定不行的……
另外,我了解得不是很细,记得用于多线程的函数是不应该有返回值的……如果真需要返回些什么也应该是通过那个指针参数来得到……
迷途的书童 2008-11-12
  • 打赏
  • 举报
回复
不行改成unsigned long 还是报这个故障:
error C2664: 'CreateThread' : cannot convert parameter 3 from 'unsigned long (void)' to 'unsigned long (__stdcall *)(void *)'
None of the functions with this name in scope match the target type
Longinc 2008-11-12
  • 打赏
  • 举报
回复
帮顶
wuyu637 2008-11-12
  • 打赏
  • 举报
回复
#include <windows.h>

DWORD WINAPI MyThread(LPVOID n)
{
printf("%d\n",n);
return 0;
}

int main(int argc, char* argv[])
{
HANDLE handle[10];
DWORD id;
for(int i=0;i <10;i++)
{
//MyThread((LPVOID)i);
handle[i]=CreateThread(
NULL, //默认保留
0, //栈大小,默认是1兆
MyThread,//线程名称(函数名称)
(LPVOID)i,//线程参数
NULL,//FLAG
&id//线程ID
);

}
BOOL RUN=FALSE;
while(1)
{
RUN=FALSE;
for(int i=0;i <10;i++)
{
DWORD exitcode;
if(::GetExitCodeThread(handle[i],&exitcode))
{
if(exitcode==STILL_ACTIVE)
{
RUN=TRUE;
break;
}
else
CloseHandle(handle[i]); //引用计数减1,等线程退出的时候再减1
}
}
if(!RUN)
break;
}

return 0;
}
xhs_lh04 2008-11-12
  • 打赏
  • 举报
回复
'unsigned long pro()
{
cout<<"Hello!"<<endl;
return 0;
}

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧