valgrind memory leak errors when using pthread_create

A thread’s resources are not immediately released at termination, unless
the thread was created with the detach state attribute set to
PTHREAD_CREATE_DETACHED, or if pthread_detach is called for
its pthread_t.

An undetached thread will remain terminated state until its identifier is passed to pthread_join or pthread_detach.

To sum it up, you have three options:

  1. create your thread with detached attribute set(PTHREAD_CREATE_DETACHED attribute)
  2. Detach your thread after creation (by calling pthread_detach), or
  3. Join with the terminated threads to recycle them (by calling pthread_join).

Hth.

Leave a Comment