退出C语言中的多线程程序时出现内存错误[已关闭]

mgdq6dx1  于 2023-01-25  发布在  其他
关注(0)|答案(1)|浏览(167)

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
14小时前关门了。
Improve this question
我在用c语言解决哲学家的就餐问题我使用的是pthread_create()和更高版本的pthread_join当一个哲学家"死了"时,我需要立即停止程序,这样其他线程就不会继续执行操作,所以我使用exit()。问题是valgrind告诉我没有释放应该从线程中释放的资源(我没有在它们里面使用任何动态内存)所以我猜是线程本身的创建没有释放,我试过使用pthread_detach,但是它也让我丢失了分配内存。我该怎么做才能在那个时候终止所有线程而不出现这个问题呢?
我在另一个函数中退出程序,该函数销毁了所有互斥锁,稍后使用exit()
创建线程的代码如下所示:

while (++i < d->philo_n)
    {

        d->philos[i].n = i;
        d->philos[i].data = d;
        d->philos[i].eat_times = 0;
        d->philos[i].last_eat = -1;
        pthread_mutex_init(&(d->philos[i].l_fork), NULL);
        if (i == d->philo_n - 1)
            d->philos[i].r_fork = &(d->philos[0].l_fork);
        else
            d->philos[i].r_fork = &(d->philos[i + 1].l_fork);
        pthread_create(&(d->philos[i].thread), NULL, routine1, &(d->philos[i]));

    }
    i = -1;
    while (++i < d->philo_n)
        if (pthread_join(d->philos[i].thread, NULL) != 0)
            return ;

valgrind错误如下:

> ==67298== 
> ==67298== HEAP SUMMARY:
> ==67298==     in use at exit: 3,264 bytes in 12 blocks
> ==67298==   total heap usage: 15 allocs, 3 frees, 4,832 bytes allocated
> ==67298== 
> ==67298== 1,360 bytes in 5 blocks are possibly lost in loss record 1 of 2
> ==67298==    at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
> ==67298==    by 0x40147D9: calloc (rtld-malloc.h:44)
> ==67298==    by 0x40147D9: allocate_dtv (dl-tls.c:375)
> ==67298==    by 0x40147D9: _dl_allocate_tls (dl-tls.c:634)
> ==67298==    by 0x4902834: allocate_stack (allocatestack.c:430)
> ==67298==    by 0x4902834: pthread_create@@GLIBC_2.34 (pthread_create.c:647)
> ==67298==    by 0x109898: fill_data (philo.c:100)
> ==67298==    by 0x109E1B: main (philo.c:191)
> ==67298== 
> ==67298== 1,904 bytes in 7 blocks are possibly lost in loss record 2 of 2
> ==67298==    at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
> ==67298==    by 0x40147D9: calloc (rtld-malloc.h:44)
> ==67298==    by 0x40147D9: allocate_dtv (dl-tls.c:375)
> ==67298==    by 0x40147D9: _dl_allocate_tls (dl-tls.c:634)
> ==67298==    by 0x4902834: allocate_stack (allocatestack.c:430)
> ==67298==    by 0x4902834: pthread_create@@GLIBC_2.34 (pthread_create.c:647)
> ==67298==    by 0x109CA7: routine1 (philo.c:166)
> ==67298==    by 0x4901B42: start_thread (pthread_create.c:442)
> ==67298==    by 0x4992BB3: clone (clone.S:100)
> ==67298== 
> ==67298== LEAK SUMMARY:
> ==67298==    definitely lost: 0 bytes in 0 blocks
> ==67298==    indirectly lost: 0 bytes in 0 blocks
> ==67298==      possibly lost: 3,264 bytes in 12 blocks
> ==67298==    still reachable: 0 bytes in 0 blocks
> ==67298==         suppressed: 0 bytes in 0 blocks
> ==67298== 
> ==67298== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
bnl4lu3b

bnl4lu3b1#

您可以简单地使用pthread_kill从即将死亡的哲学家线程执行的例程中杀死所有其他线程,只要它可以访问其他线程的ID。

相关问题