#我的问题:#
#include <sys/prctl.h>
#include <sched.h>
#include <stdio.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#include <iostream>
#include <thread>
pthread_rwlock_t qlock;
inline int set_cpu(int i)
{
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(i,&mask);
printf("thread %u, i = %d\n", pthread_self(), i);
if(-1 == pthread_setaffinity_np(pthread_self() ,sizeof(mask),&mask))
{
fprintf(stderr, "pthread_setaffinity_np erro\n");
return -1;
}
return 0;
}
int32_t set_rt_thread(const char *thread_name, uint32_t prio)
{
struct sched_param cfg_param;
int32_t ret = 0;
if (prio > 99)
{
printf("%s rt prio is too big, %u.\n", thread_name, prio);
return -EINVAL;
}
cfg_param.sched_priority = prio;
ret = sched_setscheduler(0, SCHED_RR, &cfg_param);
printf("Cur thread new scheduler = %d.\n", sched_getscheduler(0));
if(ret < 0)
{
printf("%s sched_set scheduler to SCHED_RR error.\n", thread_name);
}
else
{
printf("%s tid=%ld is rt thread now.\n", thread_name, syscall(SYS_gettid));
}
return 0;
}
int32_t set_thread_name(const char *name)
strong text{
return prctl(PR_SET_NAME, name);
}
//thread 1
void thead_input() {
std::string thread_name = "thead_input";
set_thread_name(thread_name.c_str());
set_rt_thread(thread_name.c_str(),90);
set_cpu(5);
while(1) {
pthread_rwlock_wrlock(&qlock);
printf("thead 1\n");
pthread_rwlock_unlock(&qlock);
printf("thread unlock 2\n");
usleep(100);
}
}
//thread 2
void thead_output() {
std::string thread_name = "thead_output";
set_cpu(5);
set_thread_name(thread_name.c_str());
set_rt_thread(thread_name.c_str(),99);
while(1) {
pthread_rwlock_wrlock(&qlock);
std::cout<<"thead 2"<<std::endl;
pthread_rwlock_unlock(&qlock);
usleep(100);
}
}
//main function
int main(int argc, char **argv) {
if(pthread_rwlock_init(&qlock, NULL) != 0) {
std::cout<<"pthread rw lock init fail\n"<<std::endl;
return -1;
}
std::thread t1(thead_input);
std::thread t2(thead_output);
t1.join();
t2.join();
return 0;
}
[Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/aarch64-linux-gnu/libthread_db.so.1". 0x0000007f899de31c in __GI___pthread_timedjoin_ex (threadid=547765662160, thread_return=0x0, abstime=0x0, block=<optimized out>) at pthread_join_common.c:89 89 pthread_join_common.c: No such file or directory. (gdb) thread apply all bt Thread 3 (Thread 0x7f88e0a1d0 (LWP 21439)): #0 __pthread_rwlock_wrlock_full (abstime=0x0, rwlock=0x5577ff2020 <qlock>) at pthread_rwlock_common.c:679 #1 __GI___pthread_rwlock_wrlock (rwlock=0x5577ff2020 <qlock>) at pthread_rwlock_wrlock.c:27 #2 0x0000005577fdf8b8 in thead_output () at main.cpp:88 #3 0x0000005577fdff64 in std::__invoke_impl<void, void (*)()> (__f=@0x55a376afc8: 0x5577fdf844 <thead_output()>) at /usr/include/c++/7/bits/invoke.h:60 #4 0x0000005577fdfd30 in std::__invoke<void (*)()> (__fn=@0x55a376afc8: 0x5577fdf844 <thead_output()>) at /usr/include/c++/7/bits/invoke.h:95 #5 0x0000005577fe04cc in std::thread::_Invoker<std::tuple<void (*)()> >::_M_invoke<0ul>
(这=0x55a376afc8) 在 /usr/include/c++/7/thread:234 #6 0x0000005577fe0480 在 std::thread::_Invoker >::operator() (这=0x55a376afc8) 在 /usr/include/c++/7/thread:243 #7 std::thread::_State_impl 中的 0x0000005577fe044c
::_M_运行( 这= 0x55a376afc0)位于/usr/include/c++/7/thread:186 #8 0x0000007f898fee14 在 ?? () 来自 /usr/lib/aarch64-linux-gnu/libstdc++.so.6 #9 0x0000007f899dd088 在 pthread_create.c:463 处的 start_thread (arg=0x7ff794cc5f) #10 0x0000007f897964ec 在 thread_start () 中 ../sysdeps/unix/sysv/linux/aarch64/clone.S:78
Thread 2 (Thread 0x7f8960b1d0 (LWP 21438)): #0 0x0000007f899e1ef8 in __pthread_rwlock_wrlock_full (abstime=0x0, rwlock=0x5577ff2020 <qlock>) at pthread_rwlock_common.c:595 #1 __GI___pthread_rwlock_wrlock (rwlock=0x5577ff2020 <qlock>) at pthread_rwlock_wrlock.c:27 #2 0x0000005577fdf7ec in thead_input () at main.cpp:71 #3 0x0000005577fdff64 in std::__invoke_impl<void, void (*)()> (__f=@0x55a376ae78: 0x5577fdf788 <thead_input()>) at /usr/include/c++/7/bits/invoke.h:60 #4 0x0000005577fdfd30 in std::__invoke<void (*)()> (__fn=@0x55a376ae78: 0x5577fdf788 <thead_input()>) at /usr/include/c++/7/bits/invoke.h:95 #5 0x0000005577fe04cc in std::thread::_Invoker<std::tuple<void (*)()> >::_M_invoke<0ul>
(这=0x55a376ae78) 在 /usr/include/c++/7/thread:234 #6 0x0000005577fe0480 在 std::thread::_Invoker >::operator() (这=0x55a376ae78) 在 /usr/include/c++/7/thread:243 #7 std::thread::_State_impl 中的 0x0000005577fe044c
::_M_运行( 这= 0x55a376ae70)位于/usr/include/c++/7/thread:186 #8 0x0000007f898fee14 在 ?? () 来自 /usr/lib/aarch64-linux-gnu/libstdc++.so.6 #9 0x0000007f899dd088 在 pthread_create.c:463 处的 start_thread (arg=0x7ff794cc5f) #10 0x0000007f897964ec 在 thread_start () 中 ../sysdeps/unix/sysv/linux/aarch64/clone.S:78
Thread 1 (Thread 0x7f89a75ce0 (LWP 21436)): #0 0x0000007f899de31c in __GI___pthread_timedjoin_ex (threadid=547765662160, thread_return=0x0, abstime=0x0, block=<optimized out>) at pthread_join_common.c:89 #1 0x0000007f898ff0a8 in std::thread::join() () from /usr/lib/aarch64-linux-gnu/libstdc++.so.6 #2 0x0000005577fdf9cc in main (argc=1, argv=0x7ff794ce98) at main.cpp:104
您所描述的内容在 glibc bugzilla 上至少报告了三个未解决的错误:
不幸的是,这三个仍然开放,没有任何处理的范围。我使用的是 glibc-2.26,不得不恢复这个 commit 并经历更新(降级)glibc 的痛苦,但它最终工作得很好。我的系统不再遭受 glibc 内的无限循环崩溃!