C / C ++中的实时间隔

时间:2022-02-22 21:30:01
  1. It is possible to make real time intervals in not Real-Time Linux application in C/C++?
  2. 在C / C ++中不是Real-Time Linux应用程序中可以实现实时间隔吗?

I'm writing a ADC simulator. This is an application that generates packages with certain frequency. It is important that the frequency of package generation as closely as possible corresponded to the sampling rate of ADC. Why I don't want to use sleep() and usleep() to set package generation time intervals.

我正在写一个ADC模拟器。这是一个生成具有特定频率的包的应用程序。重要的是,封装产生的频率尽可能接近ADC的采样速率。为什么我不想使用sleep()和usleep()来设置包生成时间间隔。

Thanks.

2 个解决方案

#1


3  

It is possible to make real time intervals in not Real-Time Linux application in C/C++?

在C / C ++中不是Real-Time Linux应用程序中可以实现实时间隔吗?

No... if it were, it would be a Real-Time Linux system.

不......如果是的话,那就是Real-Time Linux系统。

That said, you can probably get very close, so it depends on your intervals and tolerances. Your only serious option for sub-timeslice precision is to nail the sending thread to a core and let it spin, while keeping other processing off that core, but that's very wasteful of hardware....

也就是说,你可能会非常接近,所以它取决于你的间隔和公差。对于子时间片精度,你唯一认真的选择是将发送线程固定到核心并让它旋转,同时保持其他处理能够脱离核心,但这非常浪费硬件....

If you can afford to have latencies long enough for your sending code to be re-scheduled then you can look at setting up alarms & signal handlers, but that's potentially massively higher latency, perhaps only on relatively rare occasions where the cores have all been otherwise utilised. To assess how well this works, you've got to do real measurements under realistic system loads.

如果您能够承受足够长的延迟,以便重新安排发送代码,那么您可以查看设置警报和信号处理程序,但这可能会大大提高延迟,可能仅在相对罕见的情况下核心已经全部利用。要评估其工作情况,您必须在实际系统负载下进行实际测量。

#2


1  

The packet generator shouldn't be with the packet sender.
If you want the packets to be sent on time, you should create the packets before hand, and send them to the packer sender.

数据包生成器不应与数据包发送方一起使用。如果您希望按时发送数据包,则应事先创建数据包,然后将其发送给打包程序发件人。

So you need a thread with a work queue, and use a sleep on that thread to send the packets on time. (you can look a boost's sleep())

所以你需要一个带有工作队列的线程,并在该线程上使用sleep来按时发送数据包。 (你可以看一下助力的睡眠())

#1


3  

It is possible to make real time intervals in not Real-Time Linux application in C/C++?

在C / C ++中不是Real-Time Linux应用程序中可以实现实时间隔吗?

No... if it were, it would be a Real-Time Linux system.

不......如果是的话,那就是Real-Time Linux系统。

That said, you can probably get very close, so it depends on your intervals and tolerances. Your only serious option for sub-timeslice precision is to nail the sending thread to a core and let it spin, while keeping other processing off that core, but that's very wasteful of hardware....

也就是说,你可能会非常接近,所以它取决于你的间隔和公差。对于子时间片精度,你唯一认真的选择是将发送线程固定到核心并让它旋转,同时保持其他处理能够脱离核心,但这非常浪费硬件....

If you can afford to have latencies long enough for your sending code to be re-scheduled then you can look at setting up alarms & signal handlers, but that's potentially massively higher latency, perhaps only on relatively rare occasions where the cores have all been otherwise utilised. To assess how well this works, you've got to do real measurements under realistic system loads.

如果您能够承受足够长的延迟,以便重新安排发送代码,那么您可以查看设置警报和信号处理程序,但这可能会大大提高延迟,可能仅在相对罕见的情况下核心已经全部利用。要评估其工作情况,您必须在实际系统负载下进行实际测量。

#2


1  

The packet generator shouldn't be with the packet sender.
If you want the packets to be sent on time, you should create the packets before hand, and send them to the packer sender.

数据包生成器不应与数据包发送方一起使用。如果您希望按时发送数据包,则应事先创建数据包,然后将其发送给打包程序发件人。

So you need a thread with a work queue, and use a sleep on that thread to send the packets on time. (you can look a boost's sleep())

所以你需要一个带有工作队列的线程,并在该线程上使用sleep来按时发送数据包。 (你可以看一下助力的睡眠())