欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
libcurl 多線(xiàn)程使用注意事項(補充)

問(wèn)題

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

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


解決辦法:

在初始化libcurl的時(shí)候為openssl創(chuàng )建一個(gè)互斥鎖函數,一個(gè)回調函數傳給openss

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

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

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

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

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

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


代碼示例:

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

最關(guān)鍵就是,兩個(gè)callback的實(shí)現,還有初始化鎖(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. }  


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

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久