Search code examples
cmultithreadingmemory-leaksmutexdining-philosopher

Leaks when using pthread_detach


I'm working on Philosopher. The Philosophers project involves simulating the behavior of several philosophers sitting around a table, sharing forks to eat. The aim is to implement this simulation while respecting strict constraints on resource management (mutex, threads) and avoiding classic problems such as deadlock, starvation and race conditions. The problem is when I add a limit of meal I'm getting a leaks because of the thread that run my check_death function Here is some of my code that could help to understand the issue.

void *philo_life(void *phi)
{
    t_philo *philo;
    pthread_t t;

    philo = (t_philo *)phi;
    if (philo->n % 2 == 0)
        ft_usleep(philo->info->t_eat / 10);
    while (!is_dead(philo, 0))
    {
        pthread_create(&t, NULL, check_death, phi);
        take_fork(philo);
        philo_eat(philo);
        pthread_detach(t);
        if (philo->m_count == philo->info->n_eat)
        {
            pthread_mutex_lock(&philo->info->m_stop);
            if (++philo->info->philo_eat == philo->info->n_philo)
            {
                pthread_mutex_unlock(&philo->info->m_stop);
                is_dead(philo, 2);
            }
            pthread_mutex_unlock(&philo->info->m_stop);
            return (NULL);
        }
    }
    return (NULL);
}

int is_dead(t_philo *philo, int nb)
{
    pthread_mutex_lock(&philo->info->dead);
    if (nb)
        philo->info->stop = 1;
    if (philo->info->stop)
    {
        pthread_mutex_unlock(&philo->info->dead);
        return (1);
    }
    pthread_mutex_unlock(&philo->info->dead);
    return (0);
}

void    *check_death(void *phi)
{
    t_philo *philo;

    philo = (t_philo *)phi;
    ft_usleep(philo->info->t_die + 1);
    pthread_mutex_lock(&philo->info->m_eat);
    pthread_mutex_lock(&philo->info->m_stop);
    if (!is_dead(philo, 0) && timestamp() - \
            philo->last_eat >= (long)(philo->info->t_die))
    {
        pthread_mutex_unlock(&philo->info->m_eat);
        pthread_mutex_unlock(&philo->info->m_stop);
        print(philo, " died\n");
        is_dead(philo, 1);
    }
    pthread_mutex_unlock(&philo->info->m_eat);
    pthread_mutex_unlock(&philo->info->m_stop);
    return (NULL);
}

If you need any other function to understand feel free to ask. Here is the valgrind output :

==439355== 
==439355== HEAP SUMMARY:
==439355==     in use at exit: 1,088 bytes in 4 blocks
==439355==   total heap usage: 16 allocs, 12 frees, 5,272 bytes allocated
==439355== 
==439355== 1,088 bytes in 4 blocks are possibly lost in loss record 1 of 1
==439355==    at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==439355==    by 0x40147D9: calloc (rtld-malloc.h:44)
==439355==    by 0x40147D9: allocate_dtv (dl-tls.c:375)
==439355==    by 0x40147D9: _dl_allocate_tls (dl-tls.c:634)
==439355==    by 0x49087B4: allocate_stack (allocatestack.c:430)
==439355==    by 0x49087B4: pthread_create@@GLIBC_2.34 (pthread_create.c:647)
==439355==    by 0x401803: philo_life (philo.c:74)
==439355==    by 0x4907AC2: start_thread (pthread_create.c:442)
==439355==    by 0x4998A03: clone (clone.S:100)
==439355== 
==439355== LEAK SUMMARY:
==439355==    definitely lost: 0 bytes in 0 blocks
==439355==    indirectly lost: 0 bytes in 0 blocks
==439355==      possibly lost: 1,088 bytes in 4 blocks
==439355==    still reachable: 0 bytes in 0 blocks
==439355==         suppressed: 0 bytes in 0 blocks
==439355== 
==439355== For lists of detected and suppressed errors, rerun with: -s
==439355== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

Solution

  • To solve the problem, just set the check_death thread outside the loop.

    void *philo_life(void *phi)
    {
        t_philo *philo;
        pthread_t t;
    
        philo = (t_philo *)phi;
        pthread_create(&t, NULL, check_death, phi);
        pthread_detach(t);
        if (philo->n % 2 == 0)
            ft_usleep(philo->info->t_eat / 10);
        while (!is_dead(philo, 0))
        {
            take_fork(philo);
            philo_eat(philo);
            if (philo->m_count == philo->info->n_eat)
            {
                pthread_mutex_lock(&philo->info->m_stop);
                if (++philo->info->philo_eat == philo->info->n_philo)
                {
                    pthread_mutex_unlock(&philo->info->m_stop);
                    is_dead(philo, 2);
                }
                pthread_mutex_unlock(&philo->info->m_stop);
                return (NULL);
            }
        }
        return (NULL);
    }