How do I make a function asynchronous in C++?

后端 未结 7 553
别跟我提以往
别跟我提以往 2021-02-02 11:21

I want to call a function which will be asynchronous (I will give a callback when this task is done).

I want to do this in single thread.

相关标签:
7条回答
  • 2021-02-02 11:55

    I'm not sure I understand what you want, but if it's how to make use of a callback: It works by defining a function pointer, like this (untested):

    // Define callback signature.
    typedef void (*DoneCallback) (int reason, char *explanation);
    
    // A method that takes a callback as argument.
    void doSomeWorkWithCallback(DoneCallback done)
    {
        ...
        if (done) {
           done(1, "Finished");
        }   
    }
    
    //////
    
    // A callback
    void myCallback(int reason, char *explanation)
    {
        printf("Callback called with reason %d: %s", reason, explanation);
    }
    
    /////
    
    // Put them together
    doSomeWortkWithCallback(myCallback);
    
    0 讨论(0)
  • 2021-02-02 12:00

    There are two bits to doing this.

    Firstly, packing up the function call so that it can be executed later.

    Secondly, scheduling it.

    It is the scheduling which depends on other aspects of the implementation. If you know "when this task is done", then that's all you need - to go back and retrieve the "function call" and call it. So I am not sure this is necessarily a big problem.

    The first part is then really about function objects, or even function pointers. The latter are the traditional callback mechanism from C.

    For a FO, you might have:

    class Callback
    {
    public:
      virtual void callMe() = 0;
    };
    

    You derive from this and implement that as you see fit for your specific problem. The asyncronous event queue is then nothing more than a list<> of callbacks:

    std::list<Callback*> asyncQ; // Or shared_ptr or whatever.
    
    0 讨论(0)
  • 2021-02-02 12:04

    As others have said, you technically can't in plain C++.

    However, you can create a manager that takes your task and does time-slicing or time scheduling; with each function call, the manager uses a timer to measure the amount of time the process took; if the process took less time than scheduled, and it thinks it can finish another call and use up the remaining time without going over, it can call it again; if the function does go over the alloted time, it means the function has less time next update to run. So, this will involve creating a somewhat complex system to handle it for you.

    Or, if you have a specific platform in mind, you could use threading, or create another process to handle the work.

    0 讨论(0)
  • 2021-02-02 12:05

    The long answer involves implementing your own task scheduler and wrapping your "function" up into one or more tasks. I'm not sure you want the long answer. It certainly doesn't allow you to call something, completely forget about it, and then be notified when that thing is done; however if you are feeling ambitious, it will allow you to simulate coroutines on some level without reaching outside of standard C++.

    The short answer is that this isn't possible. Use multiple threads or multiple processes. I can give you more specific information if you divulge what OS/platform you're developing for.

    0 讨论(0)
  • 2021-02-02 12:06

    This can be done portably with modern C++ or even with old C++ and some boost. Both boost and C++11 include sophisticated facilities to obtain asynchronous values from threads, but if all you want is a callback, just launch a thread and call it.

    1998 C++/boost approach:

    #include <iostream>
    #include <string>
    #include <boost/thread.hpp>
    void callback(const std::string& data)
    {
        std::cout << "Callback called because: " << data << '\n';
    }
    void task(int time)
    {
        boost::this_thread::sleep(boost::posix_time::seconds(time));
        callback("async task done");
    }
    int main()
    {
        boost::thread bt(task, 1);
        std::cout << "async task launched\n";
        boost::this_thread::sleep(boost::posix_time::seconds(5));
        std::cout << "main done\n";
        bt.join();
    }
    

    2011 C++ approach (using gcc 4.5.2, which needs this #define)

    #define _GLIBCXX_USE_NANOSLEEP
    #include <iostream>
    #include <string>
    #include <thread>
    void callback(const std::string& data)
    {
        std::cout << "Callback called because: " << data << '\n';
    }
    void task(int time)
    {
        std::this_thread::sleep_for(std::chrono::seconds(time));
        callback("async task done");
    }
    int main()
    {
        std::thread bt(task, 1);
        std::cout << "async task launched\n";
        std::this_thread::sleep_for(std::chrono::seconds(5));
        std::cout << "main done\n";
        bt.join();
    }
    
    0 讨论(0)
  • 2021-02-02 12:07

    You can't in plain C++. You'll need to use an OS-specific mechanism, and you need a point where execution is suspended in a way that allows the OS to execute the callback. E.g. for Windows, QueueUserAPC - the callback will be executed when you e.g. SleepEx or WaitForSingleObjectEx

    0 讨论(0)
提交回复
热议问题