Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 385
yogesh Avatar asked Apr 13 '11 06:04

yogesh


People also ask

How do you make a function asynchronous?

The asynchronous function can be written in Node. js using 'async' preceding the function name. The asynchronous function returns implicit Promise as a result. The async function helps to write promise-based code asynchronously via the event-loop.

What is asynchronous function in C?

The idea is simple. The first time you call the async function, it will run like normal until it hits some form of await. Then it may return. Each time after that, the function jumps back to the await statement.

Can C be asynchronous?

With the new C++11 standard, there is std::async . Pretty much anything the machine is capable of can be done in C, you just have to do it yourself.

How do you write asynchronous code?

The asynchronous code will be written in three ways: callbacks, promises, and with the async / await keywords. Note: As of this writing, asynchronous programming is no longer done using only callbacks, but learning this obsolete method can provide great context as to why the JavaScript community now uses promises.


2 Answers

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(); } 
like image 129
Cubbi Avatar answered Sep 18 '22 05:09

Cubbi


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

like image 24
Erik Avatar answered Sep 20 '22 05:09

Erik