mirror of
https://github.com/ultimatepp/ultimatepp.git
synced 2026-08-24 22:02:44 -06:00
.reference
git-svn-id: svn://ultimatepp.org/upp/trunk@11370 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
parent
3f6ba56dce
commit
6d199c5d81
4 changed files with 52 additions and 0 deletions
37
reference/AsyncWork/AsyncWork.cpp
Normal file
37
reference/AsyncWork/AsyncWork.cpp
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
#include <Core/Core.h>
|
||||
|
||||
using namespace Upp;
|
||||
|
||||
CONSOLE_APP_MAIN
|
||||
{
|
||||
StdLogSetup(LOG_FILE|LOG_COUT);
|
||||
|
||||
auto a = Async([](int n) -> double {
|
||||
double f = 1;
|
||||
for(int i = 2; i <= n; i++)
|
||||
f *= i;
|
||||
return f;
|
||||
}, 100); // Schedules job to be executed by threadpool, returns AsyncWork for the return value and job control
|
||||
|
||||
DUMP(a.Get()); // Makes sure job is finished (can execute it if it has not started yet), returns the result
|
||||
|
||||
auto b = Async([] { throw "error"; });
|
||||
|
||||
try {
|
||||
b.Get(); // exception is propagated
|
||||
}
|
||||
catch(...) {
|
||||
LOG("Exception has been caught");
|
||||
}
|
||||
|
||||
|
||||
auto c = Async([] {
|
||||
for(;;)
|
||||
if(CoWork::IsCanceled()) {
|
||||
LOG("Work was canceled");
|
||||
break;
|
||||
}
|
||||
});
|
||||
Sleep(100); // make it chance to start
|
||||
// c destructor cancels the work (can be explicitly canceled by Cancel method too)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue