豆腐生活

豆腐的平静生活
随笔 - 129, 评论 - 906, 引用 - 60

导航

关于

豆腐制作 都是精品

每月存档

最新留言

广告

 

CreateThread 是一个Win 32API 函数,_beginthread 是一个CRT(C Run-Time)函数,他们都是实现多线城的创建的函数,而且他们拥有相同的使用方法,相同的参数列表。

但是他们有什么区别呢?

一般来说,从使用角度是没有多大的区别的,CRT函数中除了signal()函数不能在CreateThread创建的线城中使用外,其他的CRT函数都可一正常使用,但是如果在CreateThread创建的线城中使用CRT函数的话,会产生一些Memory Leak.

下面是摘自KB的原话:

SUMMARY

All C Run-time functions except the signal() function work correctly when used in threads created by the CreateThread() function. However, depending on what CRT functions are called, there may be a small memory leak when threads are terminated. Calling strlen(), for example, does not trigger the allocation of the CRT thread data-block, and calling malloc(), fopen(), _open(), strtok(), ctime(), or localtime() causes allocation of a CRT per-thread data-block, which may cause a memory leak.

MORE INFORMATION

The "Programming Techniques" manual supplied with Visual C++ 32-bit Edition states that using CreateThread() in a program that uses Libcmt.lib causes many CRT functions to fail. Actually, the only function that should not be used in a thread created with CreateThread() is the signal() function.

There are two ways to create threads. One method involves using the CRT _beginthread() or _beginthreadex() (with Visual C++ 2.0 and later); the other method involves using the CreateThread() API. All CRT functions other than the signal() function work correctly in threads created with either _beginthread() or CreateThread(). However, there are some problems involved with using CRT functions in threads created with CreateThread().

Threads that are created and terminated with the CreateThread() and ExitThread() Win32 API functions do not have memory that is allocated by the CRT for static data and static buffers cleaned up when the thread terminates. Some examples of this type of memory are static data for errno and _doserrno and the static buffers used by functions such as asctime(), ctime(), localtime(), gmtime(), and mktime(). Using CreateThread() in a program that uses the CRT (for example, links with LIBCMT.LIB) may cause a memory leak of about 70-80 bytes each time a thread is terminated.

To guarantee that all static data and static buffers allocated by the CRT are cleaned up when the thread terminates, _beginthreadex() and _endthreadex() should be used when creating a thread. The _beginthreadex() function includes the same parameters and functionality as CreateThread().
另外有个小小的测验:
 用CreateThread 创建的线城能否被CRT函数 _endthreadex() 关闭?

打印 | 张贴于 2003-11-03 10:43:00 | Tag:暂无标签

留言反馈

# CreateThread和_beginthread的区别 编辑
CreateThread和_beginthread的区别
2008-02-02 23:23:00 | [匿名:perddy]
#re: CreateThread Or _beginthread() 编辑
据说线程函数终止后的_endthread() 会多做 closehandle()的工作.
2005-02-16 11:36:00 | [匿名:yihe]
#re: CreateThread Or _beginthread() 编辑
可以参看这篇文章
2004-10-13 23:00:00 | [匿名:polyrandom]
#re: CreateThread Or _beginthread() 编辑
请问_beginthreadex创建的线程可否/如何使用TerminateThread在ThreadProc外结束线程?安全否?有否mem leaks?
2004-08-04 08:59:00 | [匿名:Jason]
#回复: CreateThread Or _beginthread() 编辑
你在讲什么东西啊,我怎么看不懂,你到底要说明什么问题?
让。。。应当避免使用。。。应当避免使用。
那到底用什么啊
2004-07-11 10:19:00 | [匿名:暗暗暗暗啊]
#回复: CreateThread Or _beginthread() 编辑
为什么sam1111说“,_beginthread()和_endthread()也是应当避免使用的”?
2004-06-15 15:16:00 | [匿名:周星星]
#回复: CreateThread Or _beginthread() 编辑
CreateThread()和_beginthreadex()在Jeffrey的《Windows核心编程》中讲的很清楚,应当尽量避免使用CreateThread()。
事实上,_beginthreadex()在内部先为线程创建一个线程特有的tiddata结构,然后调用CreateThread()。在某些非线程安全的CRT函数中会请求这个结构。如果直接使用CreateThread()的话,那些函数发现请求的tiddata为NULL,就会在现场为该线程创建该结构,此后调用EndThread()时会引起内存泄漏。_endthreadex()可以释放由CreateThread()创建的线程,实际上,在它的内部会先释放由_beginthreadex()创建的tiddata结构,然后调用EndThread()。
因此,应当使用_beginthreadex()和_endthreadex(),而避免使用CreateThread()和EndThread()。当然,_beginthread()和_endthread()也是应当避免使用的。
2003-11-03 13:24:00 | [匿名:sam1111]
对不起,目前本随笔不允许发表新评论.

Powered by: Joycode.MVC引擎 0.5.2.0