C++中的连续线程

Continuous threads in C++

本文关键字:线程 连续 C++      更新时间:2023-10-16

我想制作一个应用程序,它可以不断地侦听某种输入,而不会停顿(延迟读取,错过消息(,也不会将消息写入数据库(在我的情况下,这需要60毫秒(。

我的想法是在while(1)循环中线程化(或不线程化(侦听函数,它添加到结构的数组/向量中(在下面的代码中,它是一个单一变量int newValues(,然后检查是否有可用的消息,并生成一个线程来处理它。我还尝试从侦听函数为每个接收到的消息生成一个线程(这是一种更好的方法,但我不知道是否可以生成无限个线程(,但两次程序都以:pure virtual method called终止。我发现这意味着被破坏对象的一个成员被调用,但这并不能帮助我理解为什么下面的代码不起作用:

#include <iostream>
#include <thread>
using namespace std;
int newValues;  //if this is under 20 then it should be displayed
void listen();
void available();
int main() {
    while (1) {
        //constantly listen
        //this works for a little longer if is called without a thread
        thread lis(listen);
        lis.detach();
        if (newValues < 20) {  //if needed display it
            thread ava(available);  //without disrupting the listening
            ava.detach();
        }
    }
    return 0;
}
void listen() {
    newValues = rand() % 100;
}
void available() {
    cout << newValues << endl;
}

gdb的输出为:

[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/arm-linux-gnueabihf/libthread_db.so.1".
[New Thread 0x76bb2450 (LWP 19329)]
[New Thread 0x763b2450 (LWP 19330)]
[Thread 0x76bb2450 (LWP 19329) exited]
[Thread 0x763b2450 (LWP 19330) exited]
[New Thread 0x76bb2450 (LWP 19331)]
[Thread 0x76bb2450 (LWP 19331) exited]
[New Thread 0x763b2450 (LWP 19332)]
[Thread 0x763b2450 (LWP 19332) exited]
[New Thread 0x76bb2450 (LWP 19333)]
15
[New Thread 0x763b2450 (LWP 19334)]
[Thread 0x763b2450 (LWP 19334) exited]
[New Thread 0x75bb2450 (LWP 19335)]
pure virtual method called
terminate called without an active exception
[New Thread 0x763b2450 (LWP 19336)]
[New Thread 0x753b2450 (LWP 19337)]
pure virtual method called
terminate called recursively
[Thread 0x763b2450 (LWP 19336) exited]
Program received signal SIGABRT, Aborted.
[Switching to Thread 0x75bb2450 (LWP 19335)]
0x76bf3f50 in __GI_raise (sig=sig@entry=6)
    at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
56      ../nptl/sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /home/pi/smart_home/mw_0.1/RPi/nRF24/thread
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/arm-linux-gnueabihf/libthread_db.so.1".
[New Thread 0x76bb2450 (LWP 19341)]
[New Thread 0x763b2450 (LWP 19342)]
[Thread 0x76bb2450 (LWP 19341) exited]
[Thread 0x763b2450 (LWP 19342) exited]
[New Thread 0x76bb2450 (LWP 19343)]
pure virtual method called
terminate called without an active exception
[New Thread 0x763b2450 (LWP 19344)]
[New Thread 0x75bb2450 (LWP 19345)]
[Thread 0x763b2450 (LWP 19344) exited]
Program received signal SIGABRT, Aborted.
[Switching to Thread 0x76bb2450 (LWP 19343)]
0x76bf3f50 in __GI_raise (sig=sig@entry=6)
    at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
56      ../nptl/sysdeps/unix/sysv/linux/raise.c: No such file or directory.

实际应用程序的输出:

[New Thread 0x75bb2450 (LWP 19686)]
pure virtual method called
terminate called without an active exception  // <- difference
[New Thread 0x753b2450 (LWP 19687)]
[New Thread 0x74bb2450 (LWP 19688)]
pure virtual method called
[Thread 0x753b2450 (LWP 19687) exited]
Program received signal SIGABRT, Aborted.
[Switching to Thread 0x75bb2450 (LWP 19686)]
0x76bf3f50 in __GI_raise (sig=sig@entry=6)
    at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
56      ../nptl/sysdeps/unix/sysv/linux/raise.c: No such file or directory.

但想法是一样的,代码不会在另一台机器上工作。

我怎样才能做到这一点?

请注意,newValues上有一个具有无限线程的数据竞赛,这是未定义的行为。您需要某种同步(互斥对象或原子数据类型(。