473,320 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

_beginthreadex VS CreateThread in vc 7.1

Threads created using the CreateThread API used to leak memory if you
accesed any crt methods within the thread proc. we used to use
_beginthread to work around this issue.

Does anyone know if this has been fixed ?

-Vinayak
Nov 17 '05 #1
5 4172
Vinayak Raghuvamshi wrote:
Threads created using the CreateThread API used to leak memory if you
accesed any crt methods within the thread proc. we used to use
_beginthread to work around this issue.

Does anyone know if this has been fixed ?


The situation has not changed, nor will it ever (on Win32 at least). The
problem fundamentally is that there's no way to register a fully reliable
"atexit" mechanism that always works regardless of how the process ends,
except by using a DLL.

Under VC7.1, as was the case with previous versions of VC++, if you use the
DLL version of the CRT, your resources will in fact be reclaimed when the
CRT DLL is unloaded. If you use the static CRT, unless your program shuts
down normally, CRT resources will be leaked. In practice, there's little
consequence to this, since the OS will reclaim the resources anyway. It's
possible though that unflushed I/O buffers will be lost, etc.

In any case, using _beginthread{ex} is still the recommended procedure.

-cd
Nov 17 '05 #2
"Vinayak Raghuvamshi" <vs************@hotmail.com> wrote in message
news:9a**************************@posting.google.c om...
Threads created using the CreateThread API used to leak memory if you
accesed any crt methods within the thread proc. we used to use
_beginthread to work around this issue.

Does anyone know if this has been fixed ?


I think that there are other considerations besides the issue of leaks.

The C runtime dates back to the dark ages before it was common to have more
than a single thread of execution in a process. Some functions of the
runtime, like strtok(), for example use static data in non-thread-safe ways.
The runtime's thread creator - _beginthreadex() - insures that such data is
"instanced" for each thread. CreateThread(), because it is language neutral,
can't do that.

Of course those old string handling functions are not of much use in a
modern C++ application but if you use the runtime to create threads then you
don't have to worry about obscure problems in dark corners.

Regards,
Will
Nov 17 '05 #3
William DePalo [MVP VC++] wrote:
"Vinayak Raghuvamshi" <vs************@hotmail.com> wrote in message
news:9a**************************@posting.google.c om...
Threads created using the CreateThread API used to leak memory if you
accesed any crt methods within the thread proc. we used to use
_beginthread to work around this issue.

Does anyone know if this has been fixed ?


I think that there are other considerations besides the issue of
leaks.

The C runtime dates back to the dark ages before it was common to
have more than a single thread of execution in a process. Some
functions of the runtime, like strtok(), for example use static data
in non-thread-safe ways. The runtime's thread creator -
_beginthreadex() - insures that such data is "instanced" for each
thread.


This instancing also happens in the CRT DLL's DllMain(DLL_THREAD_ATTACH) so
if you're using the DLL CRT you get the proper instances created even if you
use CreateThread.

-cd
Nov 17 '05 #4
"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message
This instancing also happens in the CRT DLL's DllMain(DLL_THREAD_ATTACH) so if you're using the DLL CRT you get the proper instances created even if you use CreateThread.


Yup, it does, right where God intended DLLs to be initialized (to the extent
that DLLs are ever initialized). :-)

It is a side effect to be sure but favoring the CRT's _beginthreadex() over
Win32's CreateThread() keeps one who has to ask the question from hurting
himself. Naively, one might think that one could handle all of the (visible)
thread synchronization issues oneself and use CreateThread() with the singly
threaded version of the CRT. But unless one looks deep inside the runtime,
one can't be sure anything is safe.

I guess I have just never seen this issue as a bug but rather an issue of
misplaced expectations. Now, why there is a _beginthread() and a
_beginthreadex()? Well, that's something else. :-)

Regards,
Will
Nov 17 '05 #5
> I guess I have just never seen this issue as a bug but rather an issue of
misplaced expectations. Now, why there is a _beginthread() and a
_beginthreadex()? Well, that's something else. :-)


(they have different type of the start function)

Not to mension AfxBeginThread() :-)

Jan
Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: lamilla | last post by:
Salve a tutti, sto creando una applicazione che istanzia un thread al quale viene passato come argomento il puntatore ad una classe che contiene molte funzioni alcune delle quali devono essere...
1
by: phark52 | last post by:
My main app calls LoadLibrary() to load a DLL, which calls CreateThread(). This does NOT return NULL and I get a thread ID. However, ThreadProc never gets executed when this code is in the DLL. It...
3
by: raj | last post by:
Hi, I just want to know the difference between thread created by CreateThread and AfxBeginThread function. I got one problem when using the CreateThread. The appliction malfunction while using the...
3
by: mccanaveras | last post by:
Hello, I need to implement a Thread in a Visual C++ 6.0 application. I've never done this, then I've been investigating, but I don't have any properly solution. One of them is use CWinThread...
0
by: liamacheung | last post by:
Hi, I am designing a GUI application using a VC++ 2005 Form template. I have created a thread function external to the class containing the form. I can successfully create the thread and run...
5
by: eagerlearner | last post by:
Hallo, when I use class and I put one of the member function as the CreateThread argument, I keep getting the compile error in Visual Studio 2005. #include <windows.h> class X { public:...
1
by: tobycraftse | last post by:
I heard I should use the C function - CreateThread() to create thread. I have a graphics program that draws some animations. How can I add another thread to do other stuff like file processing...
17
by: Daniel | last post by:
When I use the CreateThread API method, what do I need to do when I want to pass more than one parameter where LPVOID lpParameter is passed? Daniel
3
by: sevak316 | last post by:
This must be an easy question. I am trying to create a thread in windows. I am using C as my language. I am trying to pass a function to the CreateThread function to create a thread. My...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.