mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-07 01:06:45 +08:00
add test for robust
This commit is contained in:
parent
f07fc84cb8
commit
6746e1a740
33
test/test_pthread.cpp
Executable file
33
test/test_pthread.cpp
Executable file
@ -0,0 +1,33 @@
|
|||||||
|
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
#include "test.h"
|
||||||
|
|
||||||
|
#ifdef __linux__
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
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__
|
||||||
Loading…
x
Reference in New Issue
Block a user