Do I need to clean up streams after myself in Win32 / MFC and POSIX?

I am working on a multi-threaded program using C ++ and Boost. I am using a helper thread to eagerly initialize a resource asynchronously. If I detach a stream and all links to the stream are out of scope, am I missing any resources? Or flushing the thread after itself (i.e. the stack and any other system resources needed by itself)?

From what I can see in the docs (and what I remember from pthreads 8 years ago) there is no explicit call to "destory thread" to be made.

I would like the thread to execute asynchronously, and when it comes time to use the resource, I will check if an error has occurred. A rough bit of code would look something like this:

//Assume this won't get called frequently enough that next_resource won't get promoted
//before the thread finishes.
PromoteResource() {
   current_resource_ptr = next_resource_ptr;
   next_resource_ptr.reset(new Resource());
   callable = bind(Resource::Initialize, next_resource); //not correct syntax, but I hope it clear
   boost::thread t(callable);
   t.start();
}

      

Of course, I understand that the usual memory problems still exist (forget to delete, poor exception handling, etc.) ... I just need confirmation that the stream itself is not a "leak".

Edit: point of clarification, I want to make sure this is not technically a leak:

void Run() {
   sleep(10 seconds);
}

void DoSomething(...) {
   thread t(Run);
   t.run();
} //thread detaches, will clean itself up--the thread itself isn't a 'leak'?

      

I'm sure everything is cleared after 10 seconds, but I want to be absolutely sure.

+2


source to share


4 answers


The thread's stack is cleared when it exits, but not something else. This means that whatever was allocated on the heap or elsewhere (in pre-existing data structures, for example) will remain after completion.

Also, all OS level objects (file descriptor, socket, etc.) will be left in place (unless you use a wrapper object that closes them in its destructor).



But programs that create / destroy threads frequently should probably basically deallocate whatever they allocate on the same thread, which is the only way to keep the programmer sane.

+2


source


If I'm not mistaken, on Windows Xp all resources used by a process will be released when the process exits, but this is not the case for threads.



+1


source


Yes, resources are automatically released when the thread ends. It is completely normal and acceptable to do a background thread.

In order to clean up after a stream, you must either join it or detach it (in which case you cannot join it anymore).

Here's a quote from a thread boosting thread that explains this somewhat (but not entirely).

When a boost :: thread object that represents a thread of execution, the destroyed thread becomes detached. When a thread is detached, it will continue execution until a function call or callable that is being put under construction is complete or the program terminates. The thread can also be detached by explicitly calling detach () on the boost :: thread object. In this case, the boost :: thread object no longer represents the now detached thread and instead represents a Not-a-Thread.

To wait for a thread's execution to complete, the join () or timed_join () member functions of boost :: thread must be used. join () will block the calling thread while the thread represented by the boost :: thread object has finished. If the thread of execution represented by the boost :: thread object has already terminated, or the boost :: thread object represents a Not-a-Thread, then join () returns immediately. timed_join () - Likewise, except that timed_join () will also return if the thread is waiting to terminate when the specified time has passed.

+1


source


In Win32, as soon as the main thread function called ThreadProc

in the documentation ends, the thread is flushed. Any resources you allocate internally ThreadProc

you will need to explicitly clear, of course.

+1


source







All Articles