函数在后台运行c ++ [英] function run in the background c++

查看:64
本文介绍了函数在后台运行c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个在while(1)中运行并返回整数的函数,我想在后台打开该函数并恢复其返回值.

I created a function that run in while(1) and return an integer, I want to turn this function in background and recover their return.

谁能帮助我!

这是我的功能:

int my_fct() {
    while(1) {
        int result = 1;
        return result;
    }
}

推荐答案

std :: async 以便在其他线程中对其进行计算:

How about std::async to compute it in a different thread:

int main()
{
    auto r = std::async(std::launch::async, my_fct);

    int result = r.get();    
}

需要启用C ++ 11.

Requires C++11 enabled.

这篇关于函数在后台运行c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆