4

In my programme, I handle new threads with

 pthread_t thread;
 pthread_create(&thread, NULL,
            c->someFunction, (void *) fd); //where fd is ID of the thread

The question is quite simple - if I just let the someFunction to finish, is it needed then in C++ to call something e.g. join or anything else, to prevenet memory leaks or is the memory freed automatically??

Martin Dvoracek
  • 1,714
  • 6
  • 27
  • 55

1 Answers1

7

From the opengroup page for pthread_join,

The pthread_join() function provides a simple mechanism allowing an application to wait for a thread to terminate. After the thread terminates, the application may then choose to clean up resources that were used by the thread. For instance, after pthread_join() returns, any application-provided stack storage could be reclaimed.

The pthread_join() or pthread_detach() function should eventually be called for every thread that is created with the detachstate attribute set to PTHREAD_CREATE_JOINABLE so that storage associated with the thread may be reclaimed.

and from the man page of pthread_join

Failure to join with a thread that is joinable (i.e., one that is not detached), pro‐ duces a "zombie thread". Avoid doing this, since each zombie thread consumes some system resources, and when enough zombie threads have accumulated, it will no longer be possible to create new threads (or processes).

There is no pthreads analog of waitpid(-1, &status, 0), that is, "join with any ter‐ minated thread".

If you believe you need this functionality, you probably need to rethink your application design.

If you do pthread_detach,

The pthread_detach() function shall indicate to the implementation that storage for the thread thread can be reclaimed when that thread terminates

If you don't detach or join a joinable thread, it can cause waste of resources

Suvarna Pattayil
  • 5,136
  • 5
  • 32
  • 59
  • Deleted my answer, yours is better. – BoBTFish Jun 05 '13 at 09:35
  • So when I want to exit the application, is it a good practice to iterate through e.g. a `vector` of created threads and `deatch` them (eventually `detach` certain thread after it has finished its' work) ? I'm asking because I'm not sure if I understood the difference between `join` and `detach` well. But it seems to me, that I can be sure , that `detach` will terminate the thread, just after I call it, right? – Martin Dvoracek Jun 05 '13 at 12:27
  • 4
    If you make the thread as detached, the thread's resources will be freed when the thread is done with the work. Join is used for joining to a thread (if you do a join on some thread in main, main wont finish execution until the thread is terminated and its resourcces are freed). `join` and `detach` are used for different purposes. But they both do release the resources.Detach is not for terminating the thread, thread will terminate on its own.Detached means no one is waiting for the thread to terminate. (like in join, someone is waiting for the thread to terminate so it can be joined – Suvarna Pattayil Jun 05 '13 at 12:38
  • 1
    See [this](http://stackoverflow.com/questions/3756882/detached-vs-joinable-posix-threads) discussion here too – Suvarna Pattayil Jun 05 '13 at 12:38
  • 1
    @Dworza Read [this](http://www.domaigne.com/blog/computing/joinable-and-detached-threads/) link too to know about detached threads – Suvarna Pattayil Jun 05 '13 at 13:09
  • 4
    @Dworza: For the ones that do not read long man pages: `pthread_detach()` does **not** release any resource nor terminates anything. It just configures the thread to have all its resources freed when it finally ends. – alk Jun 05 '13 at 16:32
  • @alk Ya thats much better. Small and sweet. – Suvarna Pattayil Jun 05 '13 at 17:58