[UWP] [C ++]在异步编程中管理线程上下文 [英] [UWP][C++]Managing the thread context in asynchronous programming

查看:76
本文介绍了[UWP] [C ++]在异步编程中管理线程上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我在UI线程中运行了一些异步代码,如下所示。但是,我不清楚任务和延续运行的线程(后台线程或UI线程)?



请参阅以下代码并列出问题在代码的评论中。顺便说一句,如何输出任务运​​行的线程?
$


非常感谢!



任务<空隙> initTask = create_task //此任务在后台线程中运行?

([]

{

   int i = 1;

})。然后//这个延续在后台线程中运行?

([]

{

    return create_task([]

   {

     int i = 1;

  ; });

});
$


initTask.then //这个延续在UI线程中运行?

([]

{

   std :: vector< task< void>> tasks = {create_task([] {int i = 1 ;}),&
                    create_task([] {int i = 1;}) 

                   };
$


   when_all(tasks.begin(),tasks.end())。then // this continuation在UI中运行线程?

  &NBSP; ([]

   {

     int i = 1;

  } );;
});

解决方案

您好,


要了解正在运行的代码的哪个线程,您可以尝试使用Visual Studio中的工具并调试代码,请参见下图:



"线程"调试时的工具会告诉你代码运行的是哪个线程。


所以让我们看看你的问题(这里 TppWorkerThread是线程池线程的入口点,WrapperThread是UI线程) :

 

任务< void> initTask = create_task //这个任务在后台线程中运行?

//它在WrapperThread上运行
([]
{
int i = 1;
})。然后//这个延续在后台线程中运行?

//这里它转到工作线程。
([]
{
返回create_task([]
{
int i = 1;
});
});

initTask.then //这个延续在UI线程中运行?

//这里继续转到WrapperThread
([]
{
std :: vector< task< void>> tasks = {create_task([] {int i = 1;}),
create_task([] {int i = 1; })
};

when_all(tasks.begin(),tasks.end())。然后//这个延续在UI线程中运行?

//这里它转到工作线程
([]
{
int i = 1;
});
});

顺便说一句,要输出当前的线程ID,您只需尝试以下代码:


< pre class ="prettyprint"> auto test = GetCurrentThreadId();

祝你好运,


Barry






Hi,

I have some asynchronous codes running in the UI thread as follows. However, I am not clear about which thread (background thread or UI thread) the task and continuation is running in?

Please see the following code and the questions are listed in the comments of the code. BTW, how to output the thread the task is running in?

Many thanks!

task<void> initTask = create_task//this task runs in background thread?
([]
{
    int i = 1;
}).then//this continuation runs in background thread?
([]
{
    return create_task([]
    {
        int i = 1;
    });
});

initTask.then//this continuation runs in UI thread?
([]
{
    std::vector<task<void>> tasks = { create_task([] {int i = 1; }),
                                      create_task([] {int i = 1; }) 
                                    };

    when_all(tasks.begin(), tasks.end()).then//this continuation runs in UI thread?
    ([]
    {
        int i = 1;
    });
});

解决方案

Hello,

To get to know which thread is the code running, you can try use tools from Visual Studio and debug your code, see the following image:

The "Thread" tool when debugging will tell you which thread your code is running on.

So let's see your question below(Here the TppWorkerThread is the entry-point for a thread-pool thread, WrapperThread is the UI thread):

task<void> initTask = create_task//this task runs in background thread?

//Here it runs on a WrapperThread ([] { int i = 1; }).then//this continuation runs in background thread?

//Here it goes to the worker thread. ([] { return create_task([] { int i = 1; }); }); initTask.then//this continuation runs in UI thread?

//Here it continue goes to the WrapperThread ([] { std::vector<task<void>> tasks = { create_task([] {int i = 1; }), create_task([] {int i = 1; }) }; when_all(tasks.begin(), tasks.end()).then//this continuation runs in UI thread?

//And here it goes to the worker thread ([] { int i = 1; }); });

By the way, to output the current thread ID, you can just try the following code:

auto test = GetCurrentThreadId();

Best regards,

Barry



这篇关于[UWP] [C ++]在异步编程中管理线程上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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