mirror of
https://github.com/ultimatepp/ultimatepp.git
synced 2026-05-16 14:16:09 -06:00
CoreTutorial
git-svn-id: svn://ultimatepp.org/upp/trunk@11449 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
parent
7bc2fbd3a8
commit
d586b7ce09
1 changed files with 34 additions and 14 deletions
|
|
@ -59,29 +59,49 @@ void CoWorkTutorial()
|
|||
/// negative impact on all `CoWork` instances. In fact, from this perspective, above code is
|
||||
/// probably past the threshold...
|
||||
|
||||
/// When exception is thrown in `CoWork`, it is propagated to the thread that calls `Finish`.
|
||||
/// As `CoWork` destructor calls `Finish` too, it is possible that it will be thrown by
|
||||
/// destructor, which is not exactly recommended thing to do in C++, but is well defined
|
||||
/// and really the best option here:
|
||||
/// When exception is thrown in `CoWork`, it is propagated to the thread that calls
|
||||
/// `Finish` and `CoWork` is canceled. If more than single job throws, one of exceptions is
|
||||
/// selected randomly to be rethrown in Finish.
|
||||
|
||||
/// As `CoWork` destructor calls `Finish` too,
|
||||
/// it is possible that it will be thrown by destructor, which is not exactly recommended
|
||||
/// thing to do in C++, but is well defined and really the best option here:
|
||||
|
||||
in.Seek(0);
|
||||
try {
|
||||
CoWork co;
|
||||
co & [] { throw Exc(); };
|
||||
while(!in.IsEof()) {
|
||||
String ln = in.GetLine();
|
||||
co & [ln, &w, &m] {
|
||||
if(ln.GetCount() > 75)
|
||||
throw "Input line was too long!";
|
||||
Vector<String> h = Split(ln, [](int c) { return IsAlpha(c) ? 0 : c; });
|
||||
CoWork::FinLock(); // replaces the mutex, locked till the end of CoWork job
|
||||
for(const auto& s : h)
|
||||
w.FindAdd(s);
|
||||
};
|
||||
}
|
||||
co.Finish();
|
||||
}
|
||||
catch(Exc) {
|
||||
LOG("Caught exception");
|
||||
catch(const char *exception) {
|
||||
DUMP(exception);
|
||||
}
|
||||
|
||||
/// Sometimes there is a need for cancellation of the whole `CoWork`. `Cancel` method
|
||||
/// cancels all scheduled jobs that have not been yet executed and sets `CoWork` to
|
||||
/// canceled state, which can be checked in job routine using `CoWork::IsCanceled`:
|
||||
|
||||
co & [] {
|
||||
for(;;)
|
||||
if(CoWork::IsCanceled())
|
||||
return;
|
||||
};
|
||||
for(int i = 0; i < 100; i++)
|
||||
co & [] {
|
||||
for(;;) {
|
||||
if(CoWork::IsCanceled()) {
|
||||
LOG("Job was canceled");
|
||||
return;
|
||||
}
|
||||
Sleep(1);
|
||||
}
|
||||
};
|
||||
Sleep(200); // Give CoWork a chance to start some jobs
|
||||
co.Cancel();
|
||||
|
||||
///
|
||||
/// Canceling CoWork is common in GUI applications.
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue