C++11 pthread_rwlock_wrlock在实时线程中使用,导致挂起激活,为什么要激活?

C++11 pthread_rwlock_wrlock在实时线程中使用,导致挂起激活,为什么要激活?,c++11,C++11,我的问题:# 1.我创建了两个线程,一个实时线程,一个普通线程;两个线程都有读写锁,我们将两个线程绑定到一个CPU内核上运行; 2.两个线程将挂起,有人遇到过这样的问题吗? 3.我的Glibc库是libc-2.27.so #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 pthread_rwlock_t qlock; 内联整数集\u cpu(整数i) { cpu设置屏蔽; CPU_零(和掩码); CPU_设置(i和掩码); printf(“线程%u,i=%d\n”,pthr

我的问题:#

1.我创建了两个线程,一个实时线程,一个普通线程;两个线程都有读写锁,我们将两个线程绑定到一个CPU内核上运行; 2.两个线程将挂起,有人遇到过这样的问题吗? 3.我的Glibc库是libc-2.27.so
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
pthread_rwlock_t qlock;
内联整数集\u cpu(整数i)
{
cpu设置屏蔽;
CPU_零(和掩码);
CPU_设置(i和掩码);
printf(“线程%u,i=%d\n”,pthread_self(),i);
if(-1==pthread\u setaffinity\u np(pthread\u self(),sizeof(mask),&mask))
{
fprintf(stderr,“pthread\u setaffinity\u np erro\n”);
返回-1;
}
返回0;
}
int32设置线程(常量字符*线程名称,uint32优先级)
{
结构sched_param cfg_param;
int32_t ret=0;
如果(优先级>99)
{
printf(“%s rt prio太大,%u.\n”,线程名称,prio);
返回-艾因瓦尔;
}
cfg_param.sched_priority=prio;
ret=sched_setscheduler(0、sched_RR和cfg_参数);
printf(“Cur thread new scheduler=%d.\n”,sched_getscheduler(0));
如果(ret<0)
{
printf(“%s sched_将调度程序设置为sched_RR错误。\n”,线程名称);
}
其他的
{
printf(“%s tid=%ld现在是rt线程。\n”,线程名称,syscall(SYS\u getId));
}
返回0;
}
int32设置线程名称(常量字符*名称)
强文本{
返回prctl(PR\u SET\u NAME,NAME);
}
//线程1
作废AD_输入(){
std::string thread\u name=“thead\u input”;
设置线程名称(thread\u name.c\u str());
设置线程(thread\u name.c\u str(),90);
设置cpu(5);
而(1){
pthread_rwlock_wrlock(&qlock);
printf(“thead 1\n”);
pthread_rwlock_unlock(&qlock);
printf(“线程解锁2\n”);
usleep(100);
}
}
//线程2
无效thead_输出(){
std::string thread\u name=“thead\u output”;
设置cpu(5);
设置线程名称(thread\u name.c\u str());
设置线程(thread\u name.c\u str(),99);
而(1){
pthread_rwlock_wrlock(&qlock);

std::coutFrom Review:您的问题看起来很混乱,请编辑并组织它好吗?请参阅:和。请填写此内容并添加一个我们可以回答的问题。
#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>
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>
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