C++中的连续线程

Continuous threads in C++

我想制作一个不断侦听某种输入的应用程序,没有停顿(延迟读取,错过消息)并将消息写入数据库(在我的例子中需要 60 毫秒)。

我的想法是让监听函数在 while(1) 循环中线程化(或不线程化),这将添加到结构的 array/vector 中(在下面的代码中它是单个变量 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 上有无限线程的数据竞争,这是未定义的行为。您需要某种同步(互斥锁或原子数据类型)。