This is a sample of Future
. Using Future
/Promise
allows for the simple implementation of safe synchronization and exchange of results between asynchronous processes.
PackagedTask
and Async
are written using Future
and Promise
, which allows for processes to be written in a simplified way.
using nlib_ns::threading::Future;
using nlib_ns::threading::Promise;
using nlib_ns::threading::PackagedTask;
using nlib_ns::threading::Thread;
using nlib_ns::threading::ThreadSettings;
using nlib_ns::threading::ThreadArg;
static int DeferredPlus(int x, int y) {
return x + y;
}
static int Square(Future<int>& x) {
int tmp = x.Get();
return tmp * tmp;
}
bool UsePackagedTask() {
PackagedTask<int(int, int)> task;
Future<int> future;
return false;
} else {
Thread th;
ThreadSettings settings;
settings.SetDetachState(true);
int val1 = 1;
int val2 = 2;
return false;
}
return true;
}
bool UseAsync() {
return true;
}
bool UsePromiseAndFuture() {
return true;
}
bool UseContinuation() {
return true;
}
static bool SampleMain(int, char**) {
return UsePackagedTask() && UseAsync() && UsePromiseAndFuture() && UseContinuation();
}
NLIB_MAINFUNC