gpt4 book ai didi

c - pthread_join() 在压力测试中挂起

转载 作者:太空宇宙 更新时间:2023-11-04 00:32:11 27 4
gpt4 key购买 nike

我正在运行一个 phread 测试程序,直到它失败。这是代码的主要框架:

int authSessionListMutexUnlock()
{
int rc = 0;
int rc2 = 0;

rc2 = pthread_mutex_trylock(&mutex);
ERR_IF( rc2 != EBUSY && rc2 != 0 );

rc2 = pthread_mutex_unlock(&mutex);
ERR_IF( rc2 != 0 );

cleanup:

return rc;
}

static void cleanup_handler(void *arg)
{
int rc = 0;

(void)arg;

rc = authSessionListMutexUnlock();
if (rc != 0)
AUTH_DEBUG5("authSessionListMutexUnlock() failed\n");
}


static void *destroy_expired_sessions(void *t)
{
int rc2 = 0;

(void)t;

pthread_cleanup_push(cleanup_handler, NULL);

rc2 = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
if (rc2 != 0)
AUTH_DEBUG5("pthread_setcancelstate(): rc2 == %d\n", rc2);

rc2 = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
if (rc2 != 0)
AUTH_DEBUG5("pthread_setcanceltype(): rc2 == %d\n", rc2);

while (1)
{
... // destroy expired session
sleep(min_timeout);
}
pthread_cleanup_pop(0);
}

int authDeinit( char *path )
{
...
rc2 = authSessionListDeInit();
ERR_IF( rc2 != 0 );

rc2 = pthread_cancel(destroy_thread);
ERR_IF( rc2 != 0 );

rc2 = pthread_join(destroy_thread, &status);
ERR_IF( rc2 != 0 || (int *)status != PTHREAD_CANCELED );

...
return 0
}

它在测试程序中运行良好,但测试程序在第 53743 轮与 pthread_join() 挂起:

(gdb) bt
#0 0x40000410 in __kernel_vsyscall ()
#1 0x0094aa77 in pthread_join () from /lib/libpthread.so.0
#2 0x08085745 in authDeinit ()
at /users/qixu/src/moja/auth/src//app/libauth/authAPI.c:1562
#3 0x0807e747 in main ()
at /users/qixu/src/moja/auth/src//app/tests/test_session.c:45

看起来 pthread_join() 导致了死锁。但是看代码,感觉死锁没有理由是pthread_join()造成的。当 pthread_join() 有机会运行时,唯一的互斥操作是线程本身。应该没有冲突吧?真的很困惑...

最佳答案

你的代码中至少有一个“奇怪”的地方;您的清理处理程序将始终解锁互斥量 ,即使您不是持有它的线程也是如此。

来自手册;

Calling pthread_mutex_unlock() with a mutex that the calling thread does not hold will result in undefined behavior.

关于c - pthread_join() 在压力测试中挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9777300/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com