jus*_*rxl 2 c++ multithreading visual-c++ c++-cx win-universal-app
我有一堆这样的线程任务:
create_task(somewinrtasyncfunction()).then([this(variable_that_the_last_task_returned)
{
//task here
return info;
}).then([this](info)
{
//another task here
return status});
Run Code Online (Sandbox Code Playgroud)
status现在我想在任务之外、调用它的函数中使用。我如何访问它?
您return所创建的任务(或价值)create_task(...).then(...).then(...)。
如果您需要同步获取结果,您可以尝试调用,但这.get()不会在应用程序的主 UI 线程上工作,并且不是您应该做的事情。该代码现在可能在后台线程上运行,但将来您可能最终会在 UI 线程上调用该函数(也许是通过某种非常迂回的方式),然后您的应用程序就会崩溃。修复应用程序的唯一方法是将您的代码重新设计为异步......就像它本来应该放在首位的那样。task
另请注意,尝试自行解决方法以同步获取值(例如调用WaitForSingleObjectEx()任务中发出信号的对象)可能会导致许多 WinRT 异步方法的 UI 线程死锁。不要这样做!
您应该继续异步链以对值进行操作。这是一个简单的例子;test()从主 UI 线程调用。
concurrency::task<int> get_double_async(int value)
{
return concurrency::create_task(
[value]()
{
return value * 2;
});
}
// DON'T DO THIS - IT IS A BUG FARM WAITING TO HAPPEN!
int try_calling_get()
{
auto value = 2;
value = get_double_async(value).get(); // TODO: Don't do this!
value = get_double_async(value).get(); // TODO: Don't do this!
return value;
}
concurrency::task<int> do_it_properly()
{
auto value = 2;
return get_double_async(value).then([](int value)
{
return get_double_async(value).then([](int value)
{
return value;
});
});
}
void test()
{
wchar_t buff[255];
swprintf_s(buff, L"test() being called on thread %d.\r\n", GetCurrentThreadId());
OutputDebugString(buff);
// this will fail at runtime if called from UI thread
try
{
auto x = try_calling_get();
}
catch (const concurrency::invalid_operation& op)
{
// "Illegal to wait on a task in a Windows Runtime STA"
swprintf_s(buff, L"try_calling_get() threw '%hs'; thread is %d.\r\n",
op.what(), GetCurrentThreadId());
OutputDebugString(buff);
}
// this will "work", but only because it is forced into a threadpool thread
concurrency::create_task([]
{
auto x = try_calling_get(); // TODO: Don't do this!
wchar_t buff[255];
swprintf_s(buff, L"try_calling_get() returned %d; thread is %d.\r\n",
x, GetCurrentThreadId());
OutputDebugString(buff);
});
// this is the preferred way to do it
do_it_properly().then([](int x)
{
wchar_t buff[255];
swprintf_s(buff, L"do_it_properly() returned %d; thread is %d.\r\n",
x, GetCurrentThreadId());
OutputDebugString(buff);
});
}
Run Code Online (Sandbox Code Playgroud)
请注意,此示例使用基于值的延续.then([](int value){...})而不是基于任务的延续.then([](task<int> value){...});如果您想控制异常等事情,您可以使用基于任务的延续。
这是一个示例运行(线程ID每次都会不同,有时最后两个会相同)
concurrency::task<int> get_double_async(int value)
{
return concurrency::create_task(
[value]()
{
return value * 2;
});
}
// DON'T DO THIS - IT IS A BUG FARM WAITING TO HAPPEN!
int try_calling_get()
{
auto value = 2;
value = get_double_async(value).get(); // TODO: Don't do this!
value = get_double_async(value).get(); // TODO: Don't do this!
return value;
}
concurrency::task<int> do_it_properly()
{
auto value = 2;
return get_double_async(value).then([](int value)
{
return get_double_async(value).then([](int value)
{
return value;
});
});
}
void test()
{
wchar_t buff[255];
swprintf_s(buff, L"test() being called on thread %d.\r\n", GetCurrentThreadId());
OutputDebugString(buff);
// this will fail at runtime if called from UI thread
try
{
auto x = try_calling_get();
}
catch (const concurrency::invalid_operation& op)
{
// "Illegal to wait on a task in a Windows Runtime STA"
swprintf_s(buff, L"try_calling_get() threw '%hs'; thread is %d.\r\n",
op.what(), GetCurrentThreadId());
OutputDebugString(buff);
}
// this will "work", but only because it is forced into a threadpool thread
concurrency::create_task([]
{
auto x = try_calling_get(); // TODO: Don't do this!
wchar_t buff[255];
swprintf_s(buff, L"try_calling_get() returned %d; thread is %d.\r\n",
x, GetCurrentThreadId());
OutputDebugString(buff);
});
// this is the preferred way to do it
do_it_properly().then([](int x)
{
wchar_t buff[255];
swprintf_s(buff, L"do_it_properly() returned %d; thread is %d.\r\n",
x, GetCurrentThreadId());
OutputDebugString(buff);
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1367 次 |
| 最近记录: |