如何优化运行重循环的两个线程

时间:2022-09-06 13:25:40

I have more of a conceptual question. Assume one program running two threads. Both threads are running loops all the time. One thread is responsible for streaming data and the other thread is responsible for receiving the file that the first thread has to stream. So the file transfer thread is loops to receive the data which it writes to a file and the streaming thread reads that data from that file as it needs it and streams it.

我有更多的概念问题。假设一个程序运行两个线程。这两个线程一直在运行循环。一个线程负责流数据,另一个线程负责接收第一个线程必须流的文件。因此,文件传输线程是循环来接收它写入到文件的数据,而流线程从该文件中读取数据,因为它需要它并流它。

The problem I see here is how to avoid starvation when the file transfer is taking too much CPU cycles for it's own and thus making the streaming thread lag?

我在这里看到的问题是,当文件传输占用太多CPU周期而导致流线程延迟时,如何避免出现这种情况?

How would I be able to share the CPU effectively between those two threads knowing that the streamer streams data far slower than the file transfer receives it.

我如何才能在这两个线程之间有效地共享CPU,而知道流流数据的速度要比文件传输慢得多。

I thank you for your advice.

谢谢你的建议。

2 个解决方案

#1


2  

Quite often this kind of problems are solved by using somekind of flow control:

通常这类问题都是通过某种流量控制来解决的:

Block the sender when the receiver is busy.

当接收方忙时阻塞发送方。

This cause also problems: If your program must be able to fast forward (seek forward), then this is not good idea.

这也会导致问题:如果您的程序必须能够快速前进(寻求前进),那么这不是一个好主意。

In your case, you could block the file transfer thread when there is more than 2MB unstreamed data in the file. And resume it when there is less than 1MB unstreamed data.

在您的情况下,当文件中有超过2MB的未流数据时,您可以阻塞文件传输线程。当未流数据少于1MB时重新启动。

#2


0  

See if pthread_setschedparam() helps you balance out the threads' usage of the CPU

看看pthread_setschedparam()是否有助于平衡线程对CPU的使用

From man page of pthread_setschedparam, you can change the thread priorities.

在pthread_setschedparam的man页面中,您可以更改线程优先级。

   pthread_setschedparam(pthread_t thread, int policy,
                         const struct sched_param *param);

       struct sched_param {
           int sched_priority;     /* Scheduling priority */
       };

   As can be seen, only one scheduling parameter is supported.  For details of
   the permitted ranges for scheduling priorities in each scheduling policy, see
   sched_setscheduler(2).

Also,

同时,

the file transfer is taking too much CPU cycles for it's own

文件传输占用了太多的CPU周期

If you read this SO post, it seems to suggest that changing thread priorities may not help. Because the reason the file transfer thread is consuming more CPU cycles is that it needs it. But in your case, you are OK if the file transfering is slowed down as the streamer thread cannot compete anyways! Hence I suggested you to change priorities and deprive file transfer thread of some cycles even if needs it

如果您阅读了SO这篇文章,它似乎表明改变线程优先级可能没有帮助。因为文件传输线程消耗更多CPU周期的原因是它需要它。但是在您的情况下,如果文件传输速度减慢,因为streamer线程无论如何都无法竞争,那么您是可以接受的!因此,我建议您更改优先级并取消文件传输线程的一些周期,即使需要它

#1


2  

Quite often this kind of problems are solved by using somekind of flow control:

通常这类问题都是通过某种流量控制来解决的:

Block the sender when the receiver is busy.

当接收方忙时阻塞发送方。

This cause also problems: If your program must be able to fast forward (seek forward), then this is not good idea.

这也会导致问题:如果您的程序必须能够快速前进(寻求前进),那么这不是一个好主意。

In your case, you could block the file transfer thread when there is more than 2MB unstreamed data in the file. And resume it when there is less than 1MB unstreamed data.

在您的情况下,当文件中有超过2MB的未流数据时,您可以阻塞文件传输线程。当未流数据少于1MB时重新启动。

#2


0  

See if pthread_setschedparam() helps you balance out the threads' usage of the CPU

看看pthread_setschedparam()是否有助于平衡线程对CPU的使用

From man page of pthread_setschedparam, you can change the thread priorities.

在pthread_setschedparam的man页面中,您可以更改线程优先级。

   pthread_setschedparam(pthread_t thread, int policy,
                         const struct sched_param *param);

       struct sched_param {
           int sched_priority;     /* Scheduling priority */
       };

   As can be seen, only one scheduling parameter is supported.  For details of
   the permitted ranges for scheduling priorities in each scheduling policy, see
   sched_setscheduler(2).

Also,

同时,

the file transfer is taking too much CPU cycles for it's own

文件传输占用了太多的CPU周期

If you read this SO post, it seems to suggest that changing thread priorities may not help. Because the reason the file transfer thread is consuming more CPU cycles is that it needs it. But in your case, you are OK if the file transfering is slowed down as the streamer thread cannot compete anyways! Hence I suggested you to change priorities and deprive file transfer thread of some cycles even if needs it

如果您阅读了SO这篇文章,它似乎表明改变线程优先级可能没有帮助。因为文件传输线程消耗更多CPU周期的原因是它需要它。但是在您的情况下,如果文件传输速度减慢,因为streamer线程无论如何都无法竞争,那么您是可以接受的!因此,我建议您更改优先级并取消文件传输线程的一些周期,即使需要它