valgrind memory leak errors when using pthread_create

后端 未结 5 1060
离开以前
离开以前 2020-11-30 06:58

I\'m writing a program using the pthread library. When I run my program with the command valgrind --leak-check=full, I get the following errors description:

5条回答
  •  粉色の甜心
    2020-11-30 07:17

    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.

提交回复
热议问题