Dilshan Madulanka
Dilshan Madulanka

Reputation: 25

How to run line of codes asynchronously in c++

I want to run some bunch of codes asynchronously in c++. This is for an gtk GUI application. I want to get the length from a encoder to an variable while running the other parts of the code. This lines of code should be always running. When i want the length, i should be able to get the current length from the variable. Can any one help me on this.

Upvotes: 0

Views: 444

Answers (1)

Hitesh Patil
Hitesh Patil

Reputation: 106

I haven't understood what exactly you want to do. But I think you can read more about the std::async.

#include <iostream>
#include <future>

void asyncFunction ()
{
    std::cout << "I am inside async function\n";
}

int main()
{
    std::future<void> fn = std::async(std::launch::async, asyncFunction);
    // here some other main thread operations
    return 0;
}

Function that is run asynchronously can also return a value, which can be accessed through the future with std::future::get blocking method.

Upvotes: 2

Related Questions