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

后端 未结 7 640
别跟我提以往
别跟我提以往 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 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.

提交回复
热议问题