From 6746e1a740bb53ebf207e6695fa7cf4225123b4f Mon Sep 17 00:00:00 2001 From: mutouyun Date: Sun, 6 Jun 2021 18:23:18 +0800 Subject: [PATCH] add test for robust --- test/test_pthread.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100755 test/test_pthread.cpp diff --git a/test/test_pthread.cpp b/test/test_pthread.cpp new file mode 100755 index 0000000..67b7693 --- /dev/null +++ b/test/test_pthread.cpp @@ -0,0 +1,33 @@ + +#include + +#include "test.h" + +#ifdef __linux__ +#include +#include + +TEST(PThread, Robust) { + pthread_mutexattr_t ma; + pthread_mutexattr_init(&ma); + pthread_mutexattr_setrobust(&ma, PTHREAD_MUTEX_ROBUST); + pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; + pthread_mutex_init(&mutex, &ma); + + std::thread{[&mutex] { + pthread_mutex_lock(&mutex); + // pthread_mutex_unlock(&mutex); + }}.join(); + + struct timespec tout; + clock_gettime(CLOCK_REALTIME, &tout); + struct tm *tmp = localtime(&tout.tv_sec); + tout.tv_sec += 1; /* 1 seconds from now */ + int r = pthread_mutex_timedlock(&mutex, &tout); + EXPECT_EQ(r, EOWNERDEAD); + + pthread_mutex_consistent(&mutex); + pthread_mutex_unlock(&mutex); + pthread_mutex_destroy(&mutex); +} +#endif // __linux__ \ No newline at end of file