pthread_rwlock_wrlock используются в потоках реального времени, чтобы вызвать зависание. Зачем вам жить? - PullRequest
0 голосов
/ 22 октября 2019

# моя проблема: #

1. Я создаю два потока, один поток в реальном времени, один нормальный поток;оба потока имеют блокировки чтения-записи, и мы привязываем два потока к одному ядру ЦП для запуска;

2. Два потока зависнут, кто-нибудь сталкивался с такой проблемой?

3. Моя библиотека Glibc - libc-2.27.so

#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;
}

Журнал отладки:

  1. [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>
    

    (this = 0x55a376afc8) в / usr / include /c ++ / 7 / thread: 234 # 6 0x0000005577fe0480 в std :: thread :: _ Invoker> :: operator () (this = 0x55a376afc8) в / usr / include / c ++ / 7 / thread: 243 # 7 0x0000005577fe044c в std :: thread:: _ State_impl

    :: _ M_run (this = 0x55a376afc0) в / usr / include / c ++ / 7 / thread: 186 # 8 0x0000007f898fee14 в ?? () из /usr/lib/aarch64-linux-gnu/libstdc++.so.6 # 9 0x0000007f899dd088 в start_thread (arg = 0x7ff794cc5f) по адресу pthread_create.c: 463 # 10 0x0000007f897964ec в thread_stys/unsps/undeps (at)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>
    

    (this = 0x55a376ae78) в / usr / include / c ++ / 7 / thread: 234 # 6 0x0000005577fe0480 в std:: thread :: _ Invoker> :: operator () (this = 0x55a376ae78) в / usr / include / c ++ / 7 / thread: 243 # 7 0x0000005577fe044c в std :: thread :: _ State_impl

    :: _ M_run (this = 0x55a376ae70) в / usr / include / c ++ / 7 / thread: 186 # 8 0x0000007f898fee14 in ?? () из /usr/lib/aarch64-linux-gnu/libstdc++.so.6 # 9 0x0000007f899dd088 в start_thread (arg = 0x7ff794cc5f) по адресу pthread_create.c: 463 # 10 0x0000007f897964ec в thread_stys/unsps/undeps (at)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
    
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...