C++ – Transforming C asynchronous function to C++ future

C API definition to read data from a file in asynchronous way.

void async_read_completed_callback(void* pUserData, char cont* pBuffer, int size);
void async_read_data(char const* pFilePath, void(*callback)(void* pUserData, char cont* pBuffer, int size));

C++ implementation by using future

void read_data_callback_wrapper(void* pUserData, char const* pBuffer, int size) {
    std::promise<std::vector<char>> promiseVar = std::promise<std::vector<char>> (reinterpret_cast<std::promise<std::vector<char>>*>(user_data));
    std::vector<char> data;
    for (int idx = 0; idx < size; ++idx) {

std::future<std::vector<char>> read_data_in_cpp(char const* pPath) {
    std::unique_ptr<std::promise<std::vector<char>>> promiseVar = 

    std::future<std::vector<char>> futureRes = promiseVar->get_future();

    async_read_data(pPath, reinterpret_cast<void*>(promiseVar.get()), read_data_callback_wrapper);
    return futureRes;
This entry was posted in C++, Programming. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s