一个windows下无法编译通过的多线程代码

fordream2005 2005-08-09 10:02:19
本人在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)


请多指教!!!
...全文
109 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
jinlinchen 2005-08-09
  • 打赏
  • 举报
回复
/MLd - Single-Thread Debug
/ML - Single-Thread
/MTd - Multi-Thread Debug
/MT - Multi-Thread
明白简写就好办多了
junguo 2005-08-09
  • 打赏
  • 举报
回复
我也找不到详细的vc设置的资料!
多线程要有c++多线程函数库的支持!msdn上可以找到一些内容。


healer_kx 2005-08-09
  • 打赏
  • 举报
回复
编译链接选项要对,junguo说得对,。
我没有撒啥资料,都是长期摸索出来的。
fordream2005 2005-08-09
  • 打赏
  • 举报
回复
谢谢了,这是什么原因啊,从哪里可以找到有关vc设置方面的资料啊?

真的是非常感谢
junguo 2005-08-09
  • 打赏
  • 举报
回复
你到工程-〉设置—〉C++选项下!把project option选项下的MLd改为MTd就好了!

64,654

社区成员

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

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