You can use std::promise :
std::promise<int> promise; int mm_th_done_cb(int error_code, th_result_s* th_result, void* user_data) { promise.set_value(error_code ); return 0; } int main() { std::future<int> future = promise.get_future(); request(); int value = future.get(); return 0; }
If you do not need to return any value from the callback, you can use the std::promise<void> and std::future<void> pair.
Both examples in wuqiang's answer are incorrect.
1.
#include <future> int main() { request(); // WRONG: Here we don't want to call 'mm_th_done_cb' ourselves. std::future<int> myFuture = std::async(mm_th_done_cb); //wait until mm_th_done_cb has been excuted; int result = myFuture.get(); }
2.
sinye source share