深入浅出MFC中的多线程问题

zhangxile 2008-03-23 03:03:21
#include<WINDOWS.H>
#include<PROCESS.H>
unsigned _stdcall myfunc(void* p);
#include<iostream.h>
void main()
{
unsigned long thd;
unsigned tid;

thd= _beginthreadex(NULL,
0,
myfunc,
0,
0,
&tid);
if(thd!=NULL)
{
CloseHandle(thd);
}
}
unsigned _stdcall myfunc(void* p)
{
cout<<"Inside processing"<<endl;
return 0;

}



--------------------Configuration: test - Win32 Debug--------------------
Compiling...
test.cpp
D:\Program Files\Microsoft Visual Studio\MyProjects\test001\test.cpp(18) : error C2664: 'CloseHandle' : cannot convert parameter 1 from 'unsigned long' to 'void *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.

test.exe - 1 error(s), 0 warning(s)
...全文
65 9 打赏 收藏 举报
写回复
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangxile 2008-03-23
  • 打赏
  • 举报
回复
#include<WINDOWS.H>
#include<PROCESS.H>
unsigned _stdcall myfunc(void* p);
#include<iostream.h>
void main()
{
unsigned long thd;
unsigned tid;

thd= _beginthreadex(NULL,
0,
myfunc,
0,
0,
&tid);
if(thd!=NULL)
{
CloseHandle((HANDLE)thd);
}
}
unsigned _stdcall myfunc(void* p)
{
cout<<"Inside processing"<<endl;
return 0;

}



--------------------Configuration: test - Win32 Debug--------------------
Compiling...
test.cpp
Linking...
dd.obj : error LNK2005: _main already defined in test.obj
libcimtd.lib(iostrini.obj) : error LNK2001: unresolved external symbol "void * __cdecl operator new(unsigned int,int,char const *,int)" (??2@YAPAXIHPBDH@Z)
libcimtd.lib(streamb.obj) : error LNK2001: unresolved external symbol "void * __cdecl operator new(unsigned int,int,char const *,int)" (??2@YAPAXIHPBDH@Z)
Debug/test.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

test.exe - 4 error(s), 0 warning(s)
独孤过儿 2008-03-23
  • 打赏
  • 举报
回复
再给你贴个例子:

// crt_begthrdex.cpp
// compile with: /MT
#include <windows.h>
#include <stdio.h>
#include <process.h>

unsigned Counter;
unsigned __stdcall SecondThreadFunc( void* pArguments )
{
printf( "In second thread...\n" );

while ( Counter < 1000000 )
Counter++;

_endthreadex( 0 );
return 0;
}

int main()
{
HANDLE hThread;
unsigned threadID;

printf( "Creating second thread...\n" );

// Create the second thread.
hThread = (HANDLE)_beginthreadex( NULL, 0, &SecondThreadFunc, NULL, 0, &threadID );

// Wait until second thread terminates. If you comment out the line
// below, Counter will not be correct because the thread has not
// terminated, and Counter most likely has not been incremented to
// 1000000 yet.
WaitForSingleObject( hThread, INFINITE );
printf( "Counter should be 1000000; it is-> %d\n", Counter );
// Destroy the thread object.
CloseHandle( hThread );
}


输出:
Creating second thread...
In second thread...
Counter should be 1000000; it is-> 1000000

  • 打赏
  • 举报
回复
thd 是HANDLE类型的,_beginthread的返回值需要强制转化成HANDLE类型
Supper_Jerry 2008-03-23
  • 打赏
  • 举报
回复
HANDLE hD= (HANDLE )_beginthreadex(NULL,
0,
myfunc,
0,
0,
&tid); 试一下。
独孤过儿 2008-03-23
  • 打赏
  • 举报
回复
楼主好像弄错函数的返回值了吧...

uintptr_t _beginthread(
void( *start_address )( void * ),
unsigned stack_size,
void *arglist
);
uintptr_t _beginthreadex(
void *security,
unsigned stack_size,
unsigned ( *start_address )( void * ),
void *arglist,
unsigned initflag,
unsigned *thrdaddr
);

Supper_Jerry 2008-03-23
  • 打赏
  • 举报
回复
CloseHandle不能这么使用的。查一下beginthreadex使用方法吧。
zhangxile 2008-03-23
  • 打赏
  • 举报
回复
怎么解决啊?
zhangxile 2008-03-23
  • 打赏
  • 举报
回复
我试过了,结果出现了3个error啊~~
arong1234 2008-03-23
  • 打赏
  • 举报
回复
这里是各类型问题,不知道为什么这个api用long做返回类型
你强制转换成HANDLE即可
发帖
C++ 语言

6.3w+

社区成员

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