libcurl 多線程使用注意事項(補充)——HTTPS,openssl多線程使用加鎖

問題

多線程libcurl運行一段時間後出現崩掉,沒有確定的點,沒有確定的URL。一直查看源代碼沒有問題,最後通過debug跟蹤發現是在訪問SSL的時候出現的crash。

纔想起來openssl是不支持多線程的,要自己做加鎖處理。而且libcurl中並沒有支持相關的加鎖操作。


解決辦法:

在初始化libcurl的時候爲openssl創建一個互斥鎖函數,一個回調函數傳給openss

openssl鎖l函數原形 :void (* func )(int ,int , const char * ,int)

設置方式:CRYPTO_set_locking_callback(void (* func )(int ,int , const char * ,int));

設置這樣一個函數還不夠,另外還要配置一個鎖id回調函數,這個可以參考openssl多線程下的使用相關。

id函數原形:unsigned int (*func)(void)

設置方式:CRYPTO_set_id_callback(unsigned int (*func)(void));

通過這兩個設置就可以解決HTTPS多線程請求出現crash的問題。


代碼示例:

下面是引用了libcurl示例的一個代碼

最關鍵就是,兩個callback的實現,還有初始化鎖(init_locks)和釋放鎖(kill_locks)的位置

#define USE_OPENSSL  
 
#include <stdio.h>
#include <pthread.h>
#include <curl/curl.h>
 
#define NUMT 4
 
/* we have this global to let the callback get easy access to it */ 
static pthread_mutex_t *lockarray;
 
#ifdef USE_OPENSSL
#include <openssl/crypto.h>
static void lock_callback(int mode, int type, char *file, int line)
{
  (void)file;
  (void)line;
  if (mode & CRYPTO_LOCK) {
    pthread_mutex_lock(&(lockarray[type]));
  }
  else {
    pthread_mutex_unlock(&(lockarray[type]));
  }
}
 
static unsigned long thread_id(void)
{
  unsigned long ret;
 
  ret=(unsigned long)pthread_self();
  return(ret);
}
 
static void init_locks(void)
{
  int i;
 
  lockarray=(pthread_mutex_t *)OPENSSL_malloc(CRYPTO_num_locks() *
                                            sizeof(pthread_mutex_t));
  for (i=0; i<CRYPTO_num_locks(); i++) {
    pthread_mutex_init(&(lockarray[i]),NULL);
  }
 
  CRYPTO_set_id_callback((unsigned long (*)())thread_id);
  CRYPTO_set_locking_callback((void (*)())lock_callback);
}
 
static void kill_locks(void)
{
  int i;
 
  CRYPTO_set_locking_callback(NULL);
  for (i=0; i<CRYPTO_num_locks(); i++)
    pthread_mutex_destroy(&(lockarray[i]));
 
  OPENSSL_free(lockarray);
}
#endif
 
#ifdef USE_GNUTLS
#include <gcrypt.h>
#include <errno.h>
 
GCRY_THREAD_OPTION_PTHREAD_IMPL;
 
void init_locks(void)
{
  gcry_control(GCRYCTL_SET_THREAD_CBS);
}
 
#define kill_locks()
#endif
 
/* List of URLs to fetch.*/ 
const char * const urls[]= {
  "https://www.example.com/",
  "https://www2.example.com/",
  "https://www3.example.com/",
  "https://www4.example.com/",
};
 
static void *pull_one_url(void *url)
{
  CURL *curl;
 
  curl = curl_easy_init();
  curl_easy_setopt(curl, CURLOPT_URL, url);
  /* this example doesn't verify the server's certificate, which means we
     might be downloading stuff from an impostor */ 
  curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
  curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
  curl_easy_perform(curl); /* ignores error */ 
  curl_easy_cleanup(curl);
 
  return NULL;
}
 
int main(int argc, char **argv)
{
  pthread_t tid[NUMT];
  int i;
  int error;
  (void)argc; /* we don't use any arguments in this example */ 
  (void)argv;
 
  /* Must initialize libcurl before any threads are started */ 
  curl_global_init(CURL_GLOBAL_ALL);
 
  init_locks();
 
  for(i=0; i< NUMT; i++) {
    error = pthread_create(&tid[i],
                           NULL, /* default attributes please */ 
                           pull_one_url,
                           (void *)urls[i]);
    if(0 != error)
      fprintf(stderr, "Couldn't run thread number %d, errno %d\n", i, error);
    else
      fprintf(stderr, "Thread %d, gets %s\n", i, urls[i]);
  }
 
  /* now wait for all threads to terminate */ 
  for(i=0; i< NUMT; i++) {
    error = pthread_join(tid[i], NULL);
    fprintf(stderr, "Thread %d terminated\n", i);
  }
 
  kill_locks();
 
  return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章