te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>c - Leaks when using pthread_detach - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c - Leaks when using pthread_detach - Stack Overflow

programmeradmin3浏览0评论

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)

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)
Share Improve this question asked Feb 17 at 15:33 ambroiselebsambroiselebs 1231 silver badge13 bronze badges 3
  • 1 Since you're working with detached threads, there is no barrier like pthread_join waiting for each thread to finish (and the resources returned to the system). It seems like the main thread terminates before the detached threads have completed their termination routine. – Erdal Küçük Commented Feb 17 at 16:13
  • The code presented seems needlessly convoluted. Why do you need separate check_death() threads for the philosophers? Dining Philosophers is typically implemented with just one thread per philosopher (and usually a single other thread). – John Bollinger Commented Feb 17 at 18:18
  • Yes indeed, I did it a bit strangely. – ambroiselebs Commented Feb 17 at 20:02
Add a comment  | 

1 Answer 1

Reset to default 0

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);
}
发布评论

评论列表(0)

  1. 暂无评论