473,804 Members | 3,958 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

_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 4192
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.goo gle.com...
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.goo gle.com...
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_THR EAD_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.nos pam>
wrote in message
This instancing also happens in the CRT DLL's DllMain(DLL_THR EAD_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
2627
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 necessariamente static. Non trovando nessun esempio in giro e non sapendo come referenziare le variabili globali di classe CIndiceSP nelle funzioni static della classe stessa ho utilizzato questo metodo che non mi crea nessun errore di compilazione...
1
6548
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 works fine in the standalone EXE source. I put example code below. #define MB(msg) MessageBox(0, msg, "", MB_OK|MB_ICONINFORMATION); DWORD APIENTRY tproc() {
3
15792
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 CreateThread. So is that a problem with this function? Thanks in advance Rajat
3
8514
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 to comunicate to a Dialog but it has been already created. And the other possibility is create a thread with the function CreateThread(), but it gives me an error in the third parameter: error C2664: 'CreateThread' : cannot convert parameter 3...
0
1311
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 operations in the thread function, but I want to be able to call member functions in the Form from the thread process. In order to do this, I plan on passing a pointer to the object (i.e. the form) as one of the CreateThread parameters. My...
5
8081
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: void f(); void s(){}; };
1
2287
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 -OR- internet query? Then where can I find tutorial on writing win32 threading code? any books suggestion? any sample code?
17
6877
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
7198
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 function has a return type void and paratmeter is void as well. How do I approach this? Is it something like this? static void run() { myThread = CreateThread(
0
9585
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10323
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10082
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7622
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6856
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5658
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2997
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.