-->

C++ Function Completing Before Other Function Fini

2019-07-22 11:18发布

问题:

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.

回答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
}


回答2:

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");
}