繁体   English   中英

C ++中的连续线程

[英]Continuous threads in C++

我想创建一个不断监听某种输入的应用程序,而不会暂停(延迟读取,错过消息)并将消息写入数据库(在我的情况下,这需要60毫秒)。

我的想法是让监听函数在while(1)循环中进行线程化(或不进行线程化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进行数据争夺,这是未定义的行为。 您需要某种同步(互斥或原子数据类型)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM