创建线程被警告

zhangjj2 2003-12-21 04:50:16
我在程序多线程程序中创建线程时,BoundsChecker给了我如下警告:
*********************************************************************
Description:

A thread, which makes calls to one or more run-time library functions, was created using CreateThread. A thread that uses functions from the C run-time libraries should use the _beginthread and _endthread C run-time functions for thread management rather than CreateThread and ExitThread. Failure to do so results in small memory leaks when ExitThread is called.

Note:
If the run-time library is statically linked and the module lacks debug info, it may not be possible for the call to _beginthread to be detected and this error is reported. In this situation, the error can be safely suppressed.

Sample Code:

...
DWORD WINAPI aThread(void *arg)
{
char *a = malloc (10);
free (a);
ExitThread(0);
}

void main()
{
int i;
DWORD id;
HANDLE hThread2[NUM_THREAD];

for (i = 0; i < NUM_THREAD; i++)
{
hThread2[i] = CreateThread(NULL, 0, aThread, NULL, 0, &id);
}

WaitForMultipleObjects(NUM_THREAD, hThread2, TRUE, INFINITE);

for (i = 0; i < NUM_THREAD; i++)
{
CloseHandle(hThread2[i]);
}
}
...

Repair

Use the C run-time functions _beginthread and _endthread to create and destroy threads, which use functions from the C run-time libraries.
*********************************************************************

我用的是::AfxBeginThread()函数创建线程的,请问上面的Description是什么意思?
...全文
22 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
vcforever 2003-12-22
  • 打赏
  • 举报
回复
如果你在控制台程序中用到了C Run-time library中的函数时最好用_beginthread函数来启动线程。

这种警告是Microsoft VC编译器的早期警告,Microsoft曾经在相关文档中指出_beginthread这个函数有很多的bug,用他来启动线程并不合适,你可以用_beginthreadex函数来启动一个线程。

关于CreateThread AfxBeginThread _beginthreadex这三个函数启动线程有什么区别请见
http://expert.csdn.net/Expert/topic/2396/2396966.xml?temp=.9162104

_beginthread和_beginthreadex函数的区别请见msdn中关于_beginthreadex函数的说明
也可以参考《win32多线程程序设计》

希望对你有所帮助!
ruihuahan 2003-12-21
  • 打赏
  • 举报
回复
A thread that uses functions from the C run-time libraries should use the _beginthread and _endthread C run-time functions for thread management rather than CreateThread and ExitThread.

调用C运行时函数的线程最好使用_beginthread来启动线程,他的作用是首先初始化C运行时的几个全局变量(C运行时的某些函数要用到这几个全局变量),然后调用CreateThread函数启动线程。如果你没有使用C运行时中的那几个函数,可以不理会这个警告,但最好是使用_beginthread来启动线程,这样比较安全。

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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