2

I am coding a C++ program to interact with the internet using the C++ REST SDK. I have a main function and a webCommunication function. The code is similar to below:

 void webCommunication(data, url)
 {
 //Communicate with the internet using the http_client
 //Print output
 }

 int main()
 {
 //Obtain information from user
 webCommunication(ans1, ans2);
 system("PAUSE");
 }

However, it seems that the main function is progressing before the webCommunication function is finished. If I make webCommunication a function type of string and have

cout << webCommunication(ans1, ans2) << endl;

But that still pauses and then prints the data retrieved. Normally, this would be fine, expect I am referring to the returned answer later on in the code. If the webCommunication isn't completed, the application crashes. Is there some kind of wait_until function I can use?

UPDATE: I have tried using a mutex suggested with no success. I also tried starting the function as a thread and then using the .join() with still no success.

Steven
  • 23
  • 1
  • 4

2 Answers2

1

If you declare your webCommunications() function as a

pplx::task<void> webCommunications()
{
}

Then you can use ".wait()" when calling the function. It will then wait until the function executes to continue. Looks like this:

pplx::task<void> webCommunications()
{
}

int main()
{
webCommunications().wait();
//Do other stuff
}
SeargentGen
  • 100
  • 2
  • 12
0

I think you are missing a keyword in the descriptions. ASYNCHRONOUS. This is indicating that it returns before finishing. If you need it to be synchronous, you should put a semaphore acquire right after the call and put a release into the callback code.

https://msdn.microsoft.com/en-us/library/jj950081.aspx

Modified code snippet from link above( added lock to callback ) :

// Creates an HTTP request and prints the length of the response stream.
pplx::task<void> HTTPStreamingAsync()
{
    http_client client(L"http://www.fourthcoffee.com");

    // Make the request and asynchronously process the response. 
    return client.request(methods::GET).then([](http_response response)
    {
        // Print the status code.
        std::wostringstream ss;
        ss << L"Server returned returned status code " << response.status_code() << L'.' << std::endl;
        std::wcout << ss.str();

        // TODO: Perform actions here reading from the response stream.
        auto bodyStream = response.body();

        // In this example, we print the length of the response to the     console.
        ss.str(std::wstring());
        ss << L"Content length is " << response.headers().content_length() << L" bytes." << std::endl;
        std::wcout << ss.str();

        // RELEASE lock/semaphore/etc here.
        mutex.unlock()
    });

    /* Sample output:
    Server returned returned status code 200.
    Content length is 63803 bytes.
    */
}

Note : Acquire the mutex after the function call to start web processing. Add to the callback code to release the mutex. In this way the main thread locks until the function actually finishes and then continues to 'pause'.

int main()
{
    HttpStreamingAsync();
    // Acquire lock to wait for complete
    mutex.lock();
    system("PAUSE");
}
LawfulEvil
  • 2,267
  • 24
  • 46
  • I tried creating a mutex mtx and then locking it after calling my httpRequest function and unlocking during the httpRequest function. However, it still is not working--the httpRequest function is executing the unlock before it has even finished the httpRequest. Tried putting the unlock in different locations and still same result. – Steven Apr 04 '15 at 14:25
  • Perhaps if you provide that code we can fix it further. In the snippet I provided, the mutex unlock does NOT execute until the http_response is received and processed. That is, you should see the outputs of content length and such prior to the mutex release. You should also be able to put a sleep just before the unlock to verify this or use a debugger with breakpoints to see when things happen. – LawfulEvil Apr 06 '15 at 19:08