一个windows下无法编译通过的多线程代码
本人在windows下写多线程程序时遇到编译无法通过问题,但又找不到问题所在,敬请各位指教!
源代码:
#include "winsock2.h"
#include <windows.h>
#include <iostream>
#include <process.h>
#include <time.h>
using namespace std;
class ThreadInfo
{
public:
//typedef int (__stdcall *THREAD_FUNC)(ThreadInfo *);
typedef int (*THREAD_FUNC)(ThreadInfo *);
ThreadInfo()
{
active = false;
id = 0;
func = NULL;
data = NULL;
}
// void lock() {threadLock.on();}
// void unlock() {threadLock.off();}
bool active;
int id;
THREAD_FUNC func;
void *data;
// WLock threadLock;
};
bool startThread(ThreadInfo *info)
{
info->active = true;
//typedef unsigned int (__stdcall *THREAD_FUNC)(void *);
typedef void (*THREAD_FUNC)(void *);
unsigned long h = _beginthread((THREAD_FUNC)info->func,0,info);
//unsigned long h = _beginthreadex(NULL,0,(THREAD_FUNC)info->func,info,0,NULL);
//unsigned long _beginthreadex( void *security, unsigned stack_size, unsigned ( __stdcall *start_address )( void * ), void *arglist, unsigned initflag, unsigned *thrdaddr );
//if (h == 0)
if (h == (unsigned long)-1)
return false;
else
return true;
}
void print1()
{
cout<<"111111"<<endl;
Sleep(100);
}
void print2()
{
cout<<"222222"<<endl;
Sleep(100);
}
int main()
{
ThreadInfo thread1;
ThreadInfo thread2;
thread1.func = print1;
thread2.func = print2;
startThread(thread1);
startThread(thread2);
return 0;
}
编译错误如下:
--------------------Configuration: thread_test - Win32 Debug--------------------
Compiling...
main.cpp
F:\C++Projects\ppstreaming_test\thread_test\main.cpp(41) : error C2065: '_beginthread' : undeclared identifier
F:\C++Projects\ppstreaming_test\thread_test\main.cpp(72) : error C2440: '=' : cannot convert from 'void (__cdecl *)(void)' to 'int (__cdecl *)(class ThreadInfo *)'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
F:\C++Projects\ppstreaming_test\thread_test\main.cpp(73) : error C2440: '=' : cannot convert from 'void (__cdecl *)(void)' to 'int (__cdecl *)(class ThreadInfo *)'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
Error executing cl.exe.
main.obj - 3 error(s), 0 warning(s)
请多指教!!!