Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asynchronous function call for C++

I need a hint how to implement asynchronous function calls in C/C++ ( or names of frameworks/API calls for windows and/or linux )

The use case is following: A parent thread calls a function. The function creates a child thread and returning, so call is non-blocking and parent thread can continue to do some job.

For example pthread_join to get result is not suitable, so result must be stored somewhare in heap and parent must be notified about. What I want is something like callback function in parent thread, that would be executed after child thread is ready with job.

This is surprising, but I can not find a single example in google.

Thanks for help

like image 989
iddqd Avatar asked Aug 02 '10 16:08

iddqd


People also ask

Does C support 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 call a function asynchronously in C++?

std::async. Calls fn (with args as arguments) at some point, returning without waiting for the execution of fn to complete. The value returned by fn can be accessed through the future object returned (by calling its member future::get ).

What is asynchronous programming in C?

What is asynchronous programming? Asynchronous programming is an effective way to reduce the delay or wait time that is happening in the code. It avoids the following scenario - an activity is blocked in a synchronous process, which will in turn block the entire application by blocking the other tasks from executing.


1 Answers

C++0x provides std::async for this. Here's an existing implementation, a discussion, and Wikipedia.

like image 159
Matt Joiner Avatar answered Nov 15 '22 06:11

Matt Joiner