Idling thread between curl_multi_perform

ぐ巨炮叔叔 提交于 2019-12-25 09:42:11

问题


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.

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.


回答1:


So for future reference, what I ended up doing was just idling the thread 10ms every iteration. This effectively caps the up speed to 1.6 mbps when using the default value for CURL_MAX_WRITE_SIZE in curl.h. For faster speeds I guess I'd have to either up the CURL_MAX_WRITE_SIZE define, or idle the thread for a shorter time.

Rough pseudocode:

while(runUpdateThread)
{
    curl_multi_perform(mCurlHandle, &handleCount);

    /* handle call results */

    std::this_thread::sleep_for( std::chrono::milliseconds(10) );
}



回答2:


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};
}

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.



来源:https://stackoverflow.com/questions/44389909/idling-thread-between-curl-multi-perform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!