Question: In the code below, main() function is calling request() function which inter call th\_request\_async() function which mm\_th\_done\_cb().

What will be the best and efficient way to proceed in main only after the mm\_th\_done\_cb() is executed.

DUMMY CODE

```
int mm_th_done_cb(int error_code, th_result_s* th_result, void* user_data)
{
    return 0;
}

void request()
{
    th_request_s MyItemInfo;
    strncpy(MyItemInfo.origin_path, szUrl, 1024+1);
    MyItemInfo.orientation = 0;
    MyItemInfo.func = mm_th_done_cb;
    MyItemInfo.used_cache = 1;
    th_request_async(MyItemInfo);
}

int main()
{
    request();
    // Here I need to do something only after mm_th_done_cb() has been excuted.
}

```

Answer: 
If C++11 is available,you can std::future

```
#include <future>
int main()
{
    request();
    std::future<int> myFuture = std::async(mm_th_done_cb);
    //wait until mm_th_done_cb has been excuted;
    int result = myFuture.get();
}

```

or you can use synchronization mechanism.such as condition\_variable,which is cross-platform.

```
#include <condition_variable>
std::mutex mtx;
std::condition_variable cv;
int mm_th_done_cb(int error_code, th_result_s* th_result, void* user_data)
{
    cv.notify_one();
    return 0;
}

int main()
{
    request();
    unique_lock<std::mutex> lck(mtx);
    cv.wait(lck);
    return 0;
}

```
You can use [RegisterWaitForSingleObject](http://msdn.microsoft.com/en-us/library/windows/desktop/ms685061%28v=vs.85%29.aspx)