c - pThreads如何在没有内存泄漏的情况下终止c中的线程

标签 c multithreading pthreads

我想知道如何正确终止线程而不造成任何内存泄漏。

我创建了一个更简单的程序来演示我的问题。在下面的代码中,我在 main 中创建了一个父线程,并且父线程创建了 6 个线程,但对于本示例,为了简单起见,我只完成了一个。父线程将请求用户输入,如果是 EXIT,则需要终止所有子线程。否则,如果有其他情况,它将继续处理并在最后加入。本示例的线程打印出一个字符串,然后返回。

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>

void *ParentThread(void * args);

void *oneThread(void *args);
void *twoThread(void *args);

int main(int argc, char const *argv[])
{
    
    /* Declare parent thread */
    pthread_t parent;

    /* Create parent thread */
    pthread_create(&parent, NULL, ParentThread, NULL);
    pthread_join(parent, NULL);

    return 0;
}

void *ParentThread(void * args)
{
    char sUserInput[10];

    pthread_t A;

    pthread_create(&A, NULL, oneThread, NULL);

    scanf("%10s", sUserInput);

    /* Check if input is 'EXIT' */
    if(strcmp(sUserInput, "EXIT") == 0)
    {   
        /* Terminate Threads */
        pthread_cancel(A);
    }
    else
    {
        /*
            Other actions are performed 
        */
        pthread_join(A, NULL);
    }

    return NULL;
}

void *oneThread(void *args)
{
    /* just an example to see if the thread is working */

    printf("thread one\n");

    return NULL;
}

运行程序时,它会打印出“thread one”,然后要求用户输入。如果用户输入 EXIT,它将终止线程,而其他任何内容都会加入线程。运行 Valgrind 时,我得到以下结果:

输入 - 退出

HEAP SUMMARY:
==217==     in use at exit: 272 bytes in 1 blocks
==217==   total heap usage: 4 allocs, 3 frees, 8,736 bytes allocated
==217==
==217== LEAK SUMMARY:
==217==    definitely lost: 0 bytes in 0 blocks
==217==    indirectly lost: 0 bytes in 0 blocks
==217==      possibly lost: 272 bytes in 1 blocks
==217==    still reachable: 0 bytes in 0 blocks
==217==         suppressed: 0 bytes in 0 blocks
enter code here

输入 - 测试

==220== HEAP SUMMARY:
==220==     in use at exit: 0 bytes in 0 blocks
==220==   total heap usage: 4 allocs, 4 frees, 8,736 bytes allocated
==220==
==220== All heap blocks were freed -- no leaks are possible 

如果您需要更多信息或说明,请告诉我

最佳答案

线程取消不会释放其资源:

来自man pthread_create

A thread may either be joinable or detached. If a thread is joinable, then another thread can call pthread_join(3) to wait for the thread to terminate and fetch its exit status. Only when a terminated joinable thread has been joined are the last of its resources released back to the system. When a detached thread terminates, its resources are automatically released back to the system: it is not possible to join with the thread in order to obtain its exit status.

因此,只需调用 pthread_join 或取消线程即可取回其资源并使 valgrind 满意。


另一个解决方案是将其分离。

Either pthread_join(3) or pthread_detach() should be called for each thread that an application creates, so that system resources for the thread can be released. (But note that the resources of any threads for which one of these actions has not been done will be freed when the process terminates.)

关于c - pThreads如何在没有内存泄漏的情况下终止c中的线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72084827/

相关文章:

python - 从C向Python程序发送字符串

c - 如何使用 strtok 在空格处解析字符串?

java - android多线程中的正确设计

c - 如何使用 pthread 管理 c 中的多个线程?

c - 段错误 - pthread 互斥锁定问题

条件位设置

c++ - 从头开始实现经典 OPC DA 服务器

java - 从java中的两个不同线程同时访问变量

c# - 如何从另一个线程将项目添加到 ListView 而不会导致异常

linux - 在 Linux 中控制信号量队列中的出队顺序