Last active
December 2, 2017 10:35
-
-
Save patrickdawson/da3ff09835dfc0c19caf2fd94c105f18 to your computer and use it in GitHub Desktop.
Curl Multithreading Test with Connection Pool Share (C++ 11, std)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.cpp | |
// CurlDirectTest | |
// | |
// Created by Patrick Dawson on 01.12.17. | |
// Copyright © 2017 Patrick Dawson. All rights reserved. | |
// | |
#include <iostream> | |
#include <thread> | |
#include <mutex> | |
#include "curl/curl.h" | |
using namespace std; | |
static const int NUM_THREADS = 10; | |
const size_t ITERATIONS = 100; | |
#define CHECK_CURL(cmd) \ | |
if ((cmd) != CURLE_OK) \ | |
{ \ | |
throw runtime_error( "Error executing: "#cmd ); \ | |
} | |
#define CHECK_CURLSH(cmd) \ | |
if ((cmd) != CURLSHE_OK) \ | |
{ \ | |
throw runtime_error( "Error executing: "#cmd ); \ | |
} | |
mutex g_Mutex; | |
static void ShareLockFunc( CURL* pHandle, curl_lock_data Data, curl_lock_access Access, void* pUseptr ) | |
{ | |
(void)pHandle; | |
(void)Data; | |
(void)Access; | |
(void)pUseptr; | |
g_Mutex.lock(); | |
} | |
static void ShareUnlockFunc( CURL* pHandle, curl_lock_data Data, void* pUseptr ) | |
{ | |
(void)pHandle; | |
(void)Data; | |
(void)pUseptr; | |
g_Mutex.unlock(); | |
} | |
typedef struct MyData { | |
CURLSH* pShare; | |
} MYDATA, *PMYDATA; | |
int CurlPerformGet( CURL* pHandle ) | |
{ | |
CHECK_CURL( curl_easy_setopt( pHandle, CURLOPT_FOLLOWLOCATION, 1L ) ); | |
CHECK_CURL( curl_easy_setopt( pHandle, CURLOPT_URL, "http://127.0.0.1:3000" ) ); | |
CHECK_CURL( curl_easy_perform( pHandle ) ); | |
int StatusCode = 0; | |
CHECK_CURL( curl_easy_getinfo( pHandle, CURLINFO_RESPONSE_CODE, &StatusCode ) ); | |
return StatusCode; | |
} | |
void MyThreadFunc(CURLSH* pShare) { | |
// CURL* pHandle = curl_easy_init(); | |
for (size_t i = 0; i < ITERATIONS; ++i) | |
{ | |
CURL* pHandle = curl_easy_init(); | |
CHECK_CURL(curl_easy_setopt( pHandle, CURLOPT_SHARE, pShare )); | |
cout << "Status: " << CurlPerformGet( pHandle ) << endl; | |
curl_easy_cleanup( pHandle ); | |
} | |
//curl_easy_cleanup( pHandle ); | |
} | |
int main(int argc, const char * argv[]) { | |
(void)curl_global_init( CURL_GLOBAL_ALL ); | |
CURLSH *pShare = curl_share_init(); | |
CHECK_CURLSH(curl_share_setopt( pShare, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT )); | |
CHECK_CURLSH(curl_share_setopt( pShare, CURLSHOPT_LOCKFUNC, ShareLockFunc )); | |
CHECK_CURLSH(curl_share_setopt( pShare, CURLSHOPT_UNLOCKFUNC, ShareUnlockFunc )); | |
// Create MAX_THREADS worker threads. | |
std::thread t[NUM_THREADS]; | |
//Launch a group of threads | |
for (int i = 0; i < NUM_THREADS; ++i) { | |
t[i] = std::thread(MyThreadFunc, pShare); | |
} | |
for (int i = 0; i < NUM_THREADS; ++i) { | |
t[i].join(); | |
} | |
curl_share_cleanup( pShare ); | |
curl_global_cleanup(); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment