Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading 如何在Win8 C++/Xaml应用程序中将()让与给另一个线程? 注:我使用C++,而不是C._Multithreading_Windows Runtime_C++ Cx - Fatal编程技术网

Multithreading 如何在Win8 C++/Xaml应用程序中将()让与给另一个线程? 注:我使用C++,而不是C.

Multithreading 如何在Win8 C++/Xaml应用程序中将()让与给另一个线程? 注:我使用C++,而不是C.,multithreading,windows-runtime,c++-cx,Multithreading,Windows Runtime,C++ Cx,我有一些进行计算的代码,还有一些使用结果的代码。使用结果的位已经存在于任务中,但原始计算不是——它实际上存在于主线程的App::App初始化的调用堆栈中 在过去,我会使用: while (!computationIsFinished()) std::this_thread::yield(); // or the like, depending on API 然而,对于Windows应用商店应用程序(又称WinRT,pka Metro风格)来说,这似乎并不存在。我不能使用延拓,因为使用结

我有一些进行计算的代码,还有一些使用结果的代码。使用结果的位已经存在于任务中,但原始计算不是——它实际上存在于主线程的App::App初始化的调用堆栈中

在过去,我会使用:

while (!computationIsFinished())
    std::this_thread::yield(); // or the like, depending on API
然而,对于Windows应用商店应用程序(又称WinRT,pka Metro风格)来说,这似乎并不存在。我不能使用延拓,因为使用结果的位与原始计算发生的位置无关——此外,计算也不是一项任务

搜索发现Concurrency::Context::Yield,但Windows应用商店应用程序的上下文似乎不存在


所以。。。假设我在后台线程中执行任务。我如何屈服?特别是,如何在while循环中让步?

首先,在构造函数中进行昂贵的计算通常不是一个好主意。如果是应用程序类,情况就更糟了。此外,在WinRT模型中,在主ASTA线程中执行繁重的工作是非常禁止的

您可以使用concurrency::task\u completion\u事件将不面向任务的代码与其他相关工作连接起来

例如,在长序列代码中:

...

task_completion_event<ComputationResult> tce;

task<ComputationResult> computationTask(tce); 

// This task is now tied to the completion event. 
// Pass it along to interested parties.

try
{
    auto result = DoExpensiveComputations();

    // Successfully complete the task.
    tce.set(result);
}
catch(...)
{
    // On failure, propagate the exception to continuations.
    tce.set_exception(std::current_exception());
}

...

应该可以很好地工作,但是我再次建议将计算分解为它自己的任务,并且可能在构建过程中不这样做。。。肯定是一种反模式,用于响应用户界面:

Qt只是在WinRT yield实现中使用Sleep0。

您能将计算打包到任务中吗?不,它是在主线程上初始化时需要的,我不想阻塞主线程。我知道异步编程还有其他方法,我主要想知道yield是否仍然有效。我不确定你从哪里知道这是一种昂贵的计算,但事实并非如此。对于我的问题,看起来完成事件会起作用。然而,我仍然在想:有没有办法屈服?