国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
libcurl 多線程使用注意事項(xiàng)(補(bǔ)充)

問題

多線程libcurl運(yùn)行一段時(shí)間后出現(xiàn)崩掉,沒有確定的點(diǎn),沒有確定的URL。一直查看源代碼沒有問題,最后通過debug跟蹤發(fā)現(xiàn)是在訪問SSL的時(shí)候出現(xiàn)的crash。

才想起來openssl是不支持多線程的,要自己做加鎖處理。而且libcurl中并沒有支持相關(guān)的加鎖操作。


解決辦法:

在初始化libcurl的時(shí)候?yàn)閛penssl創(chuàng)建一個(gè)互斥鎖函數(shù),一個(gè)回調(diào)函數(shù)傳給openss

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

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

設(shè)置這樣一個(gè)函數(shù)還不夠,另外還要配置一個(gè)鎖id回調(diào)函數(shù),這個(gè)可以參考o(jì)penssl多線程下的使用相關(guān)。

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

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

通過這兩個(gè)設(shè)置就可以解決HTTPS多線程請(qǐng)求出現(xiàn)crash的問題。


代碼示例:

下面是引用了libcurl示例的一個(gè)代碼

最關(guān)鍵就是,兩個(gè)callback的實(shí)現(xiàn),還有初始化鎖(init_locks)和釋放鎖(kill_locks)的位置

  1. #define USE_OPENSSL    
  2.    
  3. #include <stdio.h>  
  4. #include <pthread.h>  
  5. #include <curl/curl.h>  
  6.    
  7. #define NUMT 4  
  8.    
  9. /* we have this global to let the callback get easy access to it */   
  10. static pthread_mutex_t *lockarray;  
  11.    
  12. #ifdef USE_OPENSSL  
  13. #include <openssl/crypto.h>  
  14. static void lock_callback(int mode, int type, char *file, int line)  
  15. {  
  16.   (void)file;  
  17.   (void)line;  
  18.   if (mode & CRYPTO_LOCK) {  
  19.     pthread_mutex_lock(&(lockarray[type]));  
  20.   }  
  21.   else {  
  22.     pthread_mutex_unlock(&(lockarray[type]));  
  23.   }  
  24. }  
  25.    
  26. static unsigned long thread_id(void)  
  27. {  
  28.   unsigned long ret;  
  29.    
  30.   ret=(unsigned long)pthread_self();  
  31.   return(ret);  
  32. }  
  33.    
  34. static void init_locks(void)  
  35. {  
  36.   int i;  
  37.    
  38.   lockarray=(pthread_mutex_t *)OPENSSL_malloc(CRYPTO_num_locks() *  
  39.                                             sizeof(pthread_mutex_t));  
  40.   for (i=0; i<CRYPTO_num_locks(); i++) {  
  41.     pthread_mutex_init(&(lockarray[i]),NULL);  
  42.   }  
  43.    
  44.   CRYPTO_set_id_callback((unsigned long (*)())thread_id);  
  45.   CRYPTO_set_locking_callback((void (*)())lock_callback);  
  46. }  
  47.    
  48. static void kill_locks(void)  
  49. {  
  50.   int i;  
  51.    
  52.   CRYPTO_set_locking_callback(NULL);  
  53.   for (i=0; i<CRYPTO_num_locks(); i++)  
  54.     pthread_mutex_destroy(&(lockarray[i]));  
  55.    
  56.   OPENSSL_free(lockarray);  
  57. }  
  58. #endif  
  59.    
  60. #ifdef USE_GNUTLS  
  61. #include <gcrypt.h>  
  62. #include <errno.h>  
  63.    
  64. GCRY_THREAD_OPTION_PTHREAD_IMPL;  
  65.    
  66. void init_locks(void)  
  67. {  
  68.   gcry_control(GCRYCTL_SET_THREAD_CBS);  
  69. }  
  70.    
  71. #define kill_locks()  
  72. #endif  
  73.    
  74. /* List of URLs to fetch.*/   
  75. const char * const urls[]= {  
  76.   "https://www.example.com/",  
  77.   "https://www2.example.com/",  
  78.   "https://www3.example.com/",  
  79.   "https://www4.example.com/",  
  80. };  
  81.    
  82. static void *pull_one_url(void *url)  
  83. {  
  84.   CURL *curl;  
  85.    
  86.   curl = curl_easy_init();  
  87.   curl_easy_setopt(curl, CURLOPT_URL, url);  
  88.   /* this example doesn't verify the server's certificate, which means we 
  89.      might be downloading stuff from an impostor */   
  90.   curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);  
  91.   curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);  
  92.   curl_easy_perform(curl); /* ignores error */   
  93.   curl_easy_cleanup(curl);  
  94.    
  95.   return NULL;  
  96. }  
  97.    
  98. int main(int argc, char **argv)  
  99. {  
  100.   pthread_t tid[NUMT];  
  101.   int i;  
  102.   int error;  
  103.   (void)argc; /* we don't use any arguments in this example */   
  104.   (void)argv;  
  105.    
  106.   /* Must initialize libcurl before any threads are started */   
  107.   curl_global_init(CURL_GLOBAL_ALL);  
  108.    
  109.   init_locks();  
  110.    
  111.   for(i=0; i< NUMT; i++) {  
  112.     error = pthread_create(&tid[i],  
  113.                            NULL, /* default attributes please */   
  114.                            pull_one_url,  
  115.                            (void *)urls[i]);  
  116.     if(0 != error)  
  117.       fprintf(stderr, "Couldn't run thread number %d, errno %d\n", i, error);  
  118.     else  
  119.       fprintf(stderr, "Thread %d, gets %s\n", i, urls[i]);  
  120.   }  
  121.    
  122.   /* now wait for all threads to terminate */   
  123.   for(i=0; i< NUMT; i++) {  
  124.     error = pthread_join(tid[i], NULL);  
  125.     fprintf(stderr, "Thread %d terminated\n", i);  
  126.   }  
  127.    
  128.   kill_locks();  
  129.    
  130.   return 0;  
  131. }  


本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Libcurl多線程crash問題
基于C語言Dll調(diào)用的pythonqq的一個(gè)應(yīng)用拜年消息群發(fā)器的開發(fā) - 開發(fā) - 人日子...
向 pthread 傳遞多個(gè)參數(shù)的方法
c
C++使用libcurl做HttpClient
libcurl庫介紹
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服