在msdn中这样的例子:
是关于_beginthreadex的
// 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 );
}
我就直接将此代码拷贝到mfc程序中,进行编译时出现如下错误:
error C2276: '&' : illegal operation on bound member function expression
请问大家是怎么回事?
是关于_beginthreadex的
// 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 );
}
我就直接将此代码拷贝到mfc程序中,进行编译时出现如下错误:
error C2276: '&' : illegal operation on bound member function expression
请问大家是怎么回事?
不就结了
rror C2664: '_beginthreadex' : cannot convert parameter 3 from 'unsigned int (void *)' to 'unsigned int (__stdcall *)(void *)'
None of the functions with this name in scope match the target type
应该是UINT WINAPI SecondThreadFunc(LPVOID)
是__stdcall
改成这样
hThread = (HANDLE)_beginthreadex( NULL, 0, &SecondThreadFunc, NULL, 0, &threadID );
改成这样hThread = (HANDLE)_beginthreadex( NULL, 0, SecondThreadFunc, NULL, 0, &threadID );会出现如下问题:
rror C2664: '_beginthreadex' : cannot convert parameter 3 from 'unsigned int (void *)' to 'unsigned int (__stdcall *)(void *)'
None of the functions with this name in scope match the target type
hThread = (HANDLE)_beginthreadex( NULL, 0, SecondThreadFunc, NULL, 0, &threadID );已测试.
我在vs2005编译了一下没问题
在vc6编译要要求对Multi-threaded 的设置(vc6默认不支持)
setup:
Open the project settings dialog and select the C/C++ tab. Select the category Code Generation For the Win32 Release configuration, set Use run-time library to Multithreaded DLL.