curl_multi_perform之间的空闲线程 [英] Idling thread between curl_multi_perform

查看:210
本文介绍了curl_multi_perform之间的空闲线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个更新线程,它在while循环中运行curl_multi_perform.我想确保线程不只是坐在那里旋转,所以我想将其限制为每秒进行几次更新.这很好用,除了在10 MB以上的文件上进行文件上传或下载时,必须连续不断地调用curl_multi_perform来避免上载/下载块限制.

I have an update thread which runs curl_multi_perform in a while loop in it. I want to make sure the thread doesn't just sit there and spin, so I'd like to throttle it to a few updates per second. This works fine, except for when doing file uploads or downloads on 10+ MB files, when curl_multi_perform has to be called pretty much continuously to get around the upload/download chunk limit.

是否有一种方法可以轮询是否需要在下一个更新循环中立即再次调用curl_multi_perform,或者是否可以让线程空闲约100毫秒?我以为curl_multi_wait将用于此,但是无论如何,线程似乎在curl_multi_wait内部时正在使CPU旋转,因此似乎不正确.

Is there a way to poll if curl_multi_perform will need to be called right away again in the next update loop, or if it's okay to let the thread idle for ~100 ms? I thought curl_multi_wait would be used for this, but the thread seems to be spinning up the CPU while inside curl_multi_wait anyway, so that doesn't seem right.

推荐答案

对于您的应用程序,我建议使用sleep_until而不是sleep_for:

I recommend sleep_until instead of sleep_for for your application:

using namespace std::chrono;
auto next = steady_clock::now() + milliseconds{10};
while(runUpdateThread)
{
    curl_multi_perform(mCurlHandle, &handleCount);

    /* handle call results */

    std::this_thread::sleep_until( next );
    next += milliseconds{10};
}

现在,如果上传/下载需要很长时间,则sleep_until根本不会休眠.这样可以有效地为您提供自动节流.

Now if the upload/download takes a long time, the sleep_until doesn't sleep at all. This effectively gives you the auto throttling you are after.

这篇关于curl_multi_perform之间的空闲线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆