现在的位置: 首页 > 综合 > 正文

CURL多线程处理需要注意问题

2013年03月30日 ⁄ 综合 ⁄ 共 1593字 ⁄ 字号 评论关闭

最近用到CURL,在开启多个线程同时下载,并且又设置了timeout的时候,程序随机报如下段错误。

(gdb) bt
#0  0x00002ac0a97a2ec2 in ?? () from /usr/lib64/libcurl.so.3
#1  0x00002ac0a97a37dd in ?? () from /usr/lib64/libcurl.so.3
#2  0x00002ac0a97a4755 in curl_mvsnprintf () from /usr/lib64/libcurl.so.3
#3  0x00002ac0a97a3083 in curl_msnprintf () from /usr/lib64/libcurl.so.3
#4  0x00002ac0a9793ecb in Curl_failf () from /usr/lib64/libcurl.so.3
#5  0x00002ac0a978cf03 in Curl_resolv () from /usr/lib64/libcurl.so.3
#6  0x000000004c22197d in ?? ()
#7  0x0000000000000000 in ?? ()

经查CURL官方文档,有如下解释:

CURLOPT_NOSIGNAL

Pass a long. If it is 1, libcurl will not use any functions that install signal handlers or any functions that cause signals to be sent to the process. This option is mainly here to allow multi-threaded unix applications to still set/use all timeout options
etc, without risking getting signals. (Added in 7.10)

If this option is set and libcurl has been built with the standard name resolver, timeouts will not occur while the name resolve takes place. Consider building libcurl with c-ares support to enable asynchronous DNS lookups, which enables nice timeouts for name
resolves without signals.

Setting CURLOPT_NOSIGNAL to 1 makes libcurl NOT ask the system to ignore SIGPIPE signals, which otherwise are sent by the system when trying to send data to a
socket which is closed in the other end. libcurl makes an effort to never cause such SIGPIPEs to trigger, but some operating systems have no way to avoid them and even on those that have there are some corner cases when they may still happen, contrary to our
desire. In addition, using CURLAUTH_NTLM_WBauthentication could cause a SIGCHLD signal to be raised.

意思是说:
就是当多个线程都使用超时处理的时候,同时主线程中有sleep或是wait等操作。如果不设置这个选项,libcurl将会发信号打断这个wait从而导致程序退出。
所以,在使用的时候把这个选项设置成1就可以了.
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);


抱歉!评论已关闭.