Curl多线程https访问,崩溃问题修复

Curl: ��: SSL and multithread crash on windows, how to use mutex on windows?

SSL and multithread crash on windows, how to use mutex on windows?

From: mao mao <lmjrd_at_hotmail.com>
Date: Fri, 25 Nov 2016 09:50:48 +0000

Thankyou Thomas and Johan both


to Johan: how to use these two functions

openssl_crypto_locking_callback
openssl_crypto_init_locks
when start new thread using _beginthreadex or createthread or afxbeginthread?


Using boost:

#include <boost/ptr_container/ptr_vector.hpp>
#include <openssl/crypto.h>

#define OPENSSL_THREAD_DEFINES
#include <openssl/opensslconf.h>
#if !defined(OPENSSL_THREADS)
#error OpenSSL needs to be compiled with thread support
#endif

boost::ptr_vector<boost::mutex> openssl_lock_list;

void openssl_crypto_locking_callback(
int mode, int type, const char * const, int
) {
if( mode & CRYPTO_LOCK ) {
openssl_lock_list[type].lock();
} else {
openssl_lock_list[type].unlock();
}
}

void openssl_crypto_init_locks()
{
for(int i = 0; i < CRYPTO_num_locks(); ++i) {
openssl_lock_list.push_back(new boost::mutex());
}

// Not neccessary according to docs, and boost can't really give a pointer or
// integer ID of a thread
// CRYPTO_THREADID_set_callback(openssl_crypto_threadid_callback);
CRYPTO_set_locking_callback(openssl_crypto_locking_callback);
}



Johan de Vries
Software Developer

[http://www.ixon.net/wp-content/uploads/2013/12/logo-default.png]



> SSL and multithread crash on windows, how to use mutex on windows?

Best way is you post a striped down example which crashes.

For me the following works, without a problem.

Once:
        curl_global_init(CURL_GLOBAL_DEFAULT);

For each thread:
        _beginthread(...);
        share = curl_share_init();
        curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
        curl = curl_easy_init();
        curl_easy_setopt(curl, CURLOPT_SHARE, share);
        curl_easy_setopt(curl, CURLOPT_URL, "https://google.com");
        curl_easy_perform(curl);
        curl_easy_cleanup(curl);

Cheers,
        Thomas
-------------------------------------------------------------------
List admin: https://cool.haxx.se/list/listinfo/curl-library
Etiquette: curl - Mailing List Etiquette
 

-------------------------------------------------------------------
List admin: https://cool.haxx.se/list/listinfo/curl-library
Etiquette: curl - Mailing List Etiquette
Received on 2016-11-25

相关推荐

  1. Curl线https访问崩溃问题修复

    2023-12-22 14:28:04       47 阅读
  2. 基于curl 使用http线下载大文件

    2023-12-22 14:28:04       41 阅读
  3. 【Linux】线相关问题

    2023-12-22 14:28:04       39 阅读
  4. 线崩溃了,进程也会崩溃吗?

    2023-12-22 14:28:04       8 阅读
  5. Linux线http服务器技术点分析

    2023-12-22 14:28:04       10 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-22 14:28:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-22 14:28:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-22 14:28:04       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-22 14:28:04       20 阅读

热门阅读

  1. 基于博弈树的开源五子棋AI教程[5 启发式搜索]

    2023-12-22 14:28:04       40 阅读
  2. 前端学习——关于前端框架的思考

    2023-12-22 14:28:04       41 阅读
  3. go从0到1项目实战体系四:函数

    2023-12-22 14:28:04       45 阅读