libcurl 多线程使用注意事项(转载)
2016-03-22 12:33
555 查看
转载地址:/article/8285278.html
1、问题来源,多线程使用Libcurl导致程序跑一段时间后自己退出,没有明显的异常。找不到合适的BUG。
最后通过查看资料和网上找的一些文章,发现,原来是信号处理的问题:
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.
就是当多个线程都使用超时处理的时候,同时主线程中有sleep或是wait等操作。如果不设置这个选项,libcurl将会发信号打断这个wait从而导致程序退出。
所以,在使用的时候把这个选项设置成1就可以了.
curl_setopt(curl, CURLOPT_NOSIGNAL, 1L);
2、关于libcurl库的初始化和关闭:curl_global_init()和curl_global_cleanup()
这两个函数并不是线程安全的。所以只能在主线程中进行一次的初始化和清除。
虽然这个不是一定就会有问题,但是如果不这样处理还是有概率发生的。
1、问题来源,多线程使用Libcurl导致程序跑一段时间后自己退出,没有明显的异常。找不到合适的BUG。
最后通过查看资料和网上找的一些文章,发现,原来是信号处理的问题:
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.
就是当多个线程都使用超时处理的时候,同时主线程中有sleep或是wait等操作。如果不设置这个选项,libcurl将会发信号打断这个wait从而导致程序退出。
所以,在使用的时候把这个选项设置成1就可以了.
curl_setopt(curl, CURLOPT_NOSIGNAL, 1L);
2、关于libcurl库的初始化和关闭:curl_global_init()和curl_global_cleanup()
这两个函数并不是线程安全的。所以只能在主线程中进行一次的初始化和清除。
虽然这个不是一定就会有问题,但是如果不这样处理还是有概率发生的。
相关文章推荐
- 南阳题目40-公约数和公倍数
- CSS3 transition-property属性
- Windows程序设计(第五版)学习:第二章 Unicode简介
- HDU 1754 线段树
- css中有些属性的前面会加上“*”“_”
- java设计模式0--设计模式简介
- 去掉xcode编译warning
- 结对编程
- 人工智能的突破需要颠覆图灵机吗?
- Windows程序设计(第五版)学习:第一章 起步
- Python中的dict,List及内建函数的使用
- java中的深浅克隆
- GOOGLE PLAY转移已发布的应用到另一开发者账号
- Qt中如何添加ICON图标 Qt设置应用程序图标
- 【转】Model语句
- java继承分析
- java学习笔记16--I/O流和文件
- transient和volatile
- find . -name "Makefile" -exec grep -H "MISMATCH" {} \;
- 前言