如何在 C++ 中异步 运行 行代码

How to run line of codes asynchronously in c++

我想 运行 在 C++ 中异步编写一些代码。这是一个 gtk GUI 应用程序。我想获得从编码器到变量的长度,同时 运行 连接代码的其他部分。这行代码应该总是 运行ning。当我想要长度时,我应该能够从变量中获取当前长度。谁能帮我解决这个问题。

我还没明白你到底想做什么。但我想你可以阅读更多关于 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;
}

异步 运行 函数也可以 return 一个值,以后可以使用 std::future::get 阻塞方法访问该值。