The following is a class for curl wrapper.
This class will be called like:
class curlWrapper {
private:
CURLcode curlCode;
CURL* curlHandle;
struct curl_slist *header = NULL;
MemoryStructA response;
static std::atomic<int> numberOfCurlProcess;
static std::atomic<bool> isGlobalInitCalled;
static std::mutex curlGlobalkey;
public:
curlWrapper(){
curlGlobalkey.lock();
if(!isGlobalInitCalled)
{
curl_global_init(CURL_GLOBAL_ALL); // this should be called only once in the entire program when a first object using this class.
numberOfCurlProcess++;
}
else
{
numberOfCurlProcess++;
}
curlGlobalkey.unlock();
//globalCurlInit();
initializeMemoryStruct();
}
~curlWrapper(){
curlGlobalkey.lock();
if(numberOfCurlProcess > 0)
{
numberOfCurlProcess--;
}
else if(numberOfCurlProcess == 0)
{
// this should be called only once in the entire program when a last object using this class.
curl_global_cleanup();
}
curlGlobalkey.unlock();
}
int trace(CURL *handle, curl_infotype type, char *data, size_t size, void *classPointer);
int traceImpl(CURL *handle, curl_infotype type, char *data, size_t size);
CURL* curlInit();
void curlClose();
bool initializeConnection();
bool sendRequest(const char* request);
char* getResponse();
void setMemorytoWrite();
bool listen();
void closeConnection();
bool setURL(const char* url);
bool setHttpHeader(const char * contentType, const char * charSet);
void setTrace(bool trace);// If true trace will be displayd
bool curlExec();
void initializeMemoryStruct();
void globalCurlInit();
void globalCulrClean();
};
This class will be used as:
class Accessor{ public: curlWrapper *curl = new curlWrapper() }; main(){ //somewhere in the main Accessor aa = new Accessor(); Accessor bb = new Accessor(); thread(aa).doProcess(); thread(bb).dpProcess() }
Did I call curl_global_init(CURL_GLOBAL_ALL)
at first thread starts and only once? Similarly, did I call curl_global_cleanup()
while the last thread ends?
cProfile
like here. \$\endgroup\$