3

I'm trying to make use of libcurl with multiple threads. While reading the documentation, I understood the correct way of doing it is to use a single CURL* handle for each thread.

This is what I'm trying to do:

static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
    ((std::string*)userp)->append((char*)contents, size * nmemb);
    return size * nmemb;
}

bool KeyIsValid(std::string keytocheck) {
    CURL *curl;
    CURLcode res;
    std::string content;

    curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "http://localhost/mypage.php");
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content);

        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, ("something=hello&somethingtwo=" + keytocheck).c_str());
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);

        // std::cout << content << std::endl;
        if (content.find("id or key is not correct") == std::string::npos) // if i use an != the correct key (abc) doesn't get printed
        {
            return false;
        }
        else {
            return true;
        }
    }
}

Summarizing this code, I can say I'm working in a new handle for each thread. It makes the request to my localhost and, after the post request is performed, using a callback I'm storing the content to a std::string. After everything I check if the webpage contains some identifiers for in/corrected id/key. Exactly, the page prints out this:

id or key is not correct

when the id/key is not correct. This is how I call the method KeyIsValid():

if (KeyIsValid(currentKey))
        {
            std::cout << "key tested with success -> " << currentKey << '\n';
            return 1; // 1 = success
        }

but while I check every key stored in an array (1 stored key in the array equals to 1 new thread), I get some "misinterpretations":

key tested with success -> abc
key tested with success -> hello
key tested with success -> hello
key tested with success -> hello

while the only correct key is only abc. I'm not sure why the program prints the correct key abc followed from the other incorrect keys. But if I change the array's items to just two items (abc and hello, and so using two threads), everything seems to work properly as I get printed just the abc key.

I did some searches in internet, this is what I found:

I have a question regarding the safety of performing parallel HTTP-requests using libcurl (C++). When reading this question, please bear in mind I have limited knowledge about HTTP-requests in general. Basically, let's say I have two (or more) threads, each thread makes a HTTP-request once per second. (All the requests made are to the same server). How does my program (or something else?) keep track of what HTTP-response belongs to which tread? I mean, can I be sure that if request A was sent from thread 1, and request B from thread 2 at the same time, and the responses are retrived at the same time, the correct reponse (response A) goes to thread 1 and response B to thread 2? Please excuse my ignorance in this matter. Thanks.

this guy is just asking my same question without being more specific (he didn't show any code).

I'm going to exactly ask this:

Can I be sure that if request A was sent from thread 1, and request B from thread 2 at the same time, and the responses are retrived at the same time, the correct reponse (response A) goes to thread 1 and response B to thread 2?

with reference to my code. Maybe I'm analyzing the page incorrectly, I don't know.

Sorry for my ignorance in this matter.

Edit:

  • After two days I tried to change my callback code, but still nothing works properly.
Community
  • 1
  • 1

1 Answers1

1

Can I be sure that if request A was sent from thread 1, and request B from thread 2 at the same time, and the responses are retrieved at the same time, the correct response (response A) goes to thread 1 and response B to thread 2?

Yes you can be absolutely sure of that.

Daniel Stenberg
  • 54,736
  • 17
  • 146
  • 222