Not logged in, Join Here! or Log In Below:  
News Articles Search    

 Home / General Programming / Thread question... Account Manager
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.

May 25, 2005, 06:48 AM

I am creating two threads with CreateThread in a method... My question is what happens to the threads when the method ends and the scope where the thread was created becomes 'out of scope' if you see what I mean.

Here's some psuedo to explain my situation...
void Interface::NextScreen()
HANDLE Handle1 = CreateThread(yadda, yadda

HANDLE Handle2 = CreateThread(yadda, yadda
// What happens to the threads when this method exits?

What I want to happen is just start these threads off running and then forget about them, is this going to happen or do I need to keep track of the threads in the interface object?

Thanks for any help, and apologies if this doesn't make much sense, I am new to this area of C++.

Mark Coleman


May 25, 2005, 07:18 AM

Mmm... threads aren't really C++ per se, so your threads still persist even though the handles to them die... only the handles are destructed, since C++ doesn't know or care about what those handles point to (in this case, probably a pointer or index into some thread table)...

what you could do is... create a class called thread, and handle the creation/destruction in the class... that way you simply declare a thread, it is created, or you could explicitly call create()... when the class gets killed, the destructor releases the thread.


May 25, 2005, 08:34 AM

Your threads are created with CreateThread. They start immediately only if you don't specify the flag that puts them in suspention. It is better the app close the threads before exiting, but you can also make the threads kill themselves with ExitThread.


May 25, 2005, 10:51 AM

What you will most likely want to do is keep the handes of those threads just in case you need them later on in the program. Maybe you should make them global variables or use an object to keep track of all your threads. If they go out of scope now and you need to mess with those threads later on, you're out of luck. But when the program closes so do your threads so you don't have to worry about that. But what you do really depends on your program and how they are specifically used.

This thread contains 4 messages.
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.