C++中的连续线程
Continuous threads in C++
我想制作一个应用程序,它可以不断地侦听某种输入,而不会停顿(延迟读取,错过消息(,也不会将消息写入数据库(在我的情况下,这需要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
上有一个具有无限线程的数据竞赛,这是未定义的行为。您需要某种同步(互斥对象或原子数据类型(。
相关文章:
- 从不同线程使用int64的不同字节安全吗
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 在C++中使用cURL和多线程
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在cuda线程之间共享大量常量数据
- 如何将元素添加到数组的线程安全函数?
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 异常属于C++中的线程还是进程
- C++中的线程安全删除
- C++使用params创建线程函数会导致转换错误
- 类与私有变量的其他类之间的线程安全性
- 使用多线程获得连续的蜂鸣声
- main() 对连续运行的线程中的编辑值具有只读访问权限 - C++
- 更好的做法:永远循环线程或连续线程
- 从QTcpSocket上的数据流中连续运行复杂算法的最佳Qt线程解决方案是什么
- C++中的连续线程
- Poco::线程挂起在多个启动和加入快速连续
- 通过交替渲染或两个单独的线程,在同一个wxPanel上连续渲染两个wxglcanvas