Best way for waiting for callback to finish

前端 未结 3 1694
渐次进展
渐次进展 2021-02-08 03:14

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

3条回答
  •  悲&欢浪女
    2021-02-08 03:41

    You can use std::promise:

    std::promise promise;
    
    int mm_th_done_cb(int error_code, th_result_s* th_result, void* user_data)
    {
        promise.set_value(error_code /*this value will be returned by the future.get()*/);
        return 0;
    }
    
    int main()
    {
        std::future future = promise.get_future();
        request();
        int value = future.get();
        return 0;
    }
    

    If you don't need to return any value from the callback, then you can use a std::promise and std::future pair.

    Both examples in wuqiang's answer are wrong.

    1.

    #include 
    int main()
    {
        request();
    
        // WRONG: Here we don't want to call 'mm_th_done_cb' ourselves.
        std::future myFuture = std::async(mm_th_done_cb);
    
        //wait until mm_th_done_cb has been excuted;
        int result = myFuture.get();
    }
    

    2.

    #include 
    
    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();
    
        // WRONG: If the 'request' finishes quickly, then the 'mm_th_done_cb'
        // callback will be called and will notify the condition variable before 
        // the following lines execute, i.e. before the main thread starts 
        // waiting on the condition variable. Thus the 'cv.wait(lck)' will 
        // never return.
    
        unique_lock lck(mtx);
        cv.wait(lck);
        return 0;
    }
    

提交回复
热议问题