Home » U++ Library support » U++ MT-multithreading and servers » AsyncWork, IsFinished() may not be working properly
Re: AsyncWork, IsFinished() may not be working properly [message #54431 is a reply to message #54430] |
Sat, 18 July 2020 15:31 |
Oblivion
Messages: 1093 Registered: August 2007
|
Senior Contributor |
|
|
Happy to help
Quote:if there is a way to return from mirek's non-blocking example value
Which example, exactly, are you referring to?
The above code (in which IsFinished() method is called) is already getting the value without any locking mechanism, and is non-blocking. IsFinished() method allows you to check the progress of a given worker instance without waiting on it (i.e. non-blocking). Thus, if the work is finished then Calling Get() won't wait either, it will immediately return the resultitng value of the offloaded computation.
Let me put it this way:
CONSOLE_APP_MAIN
{
StdLogSetup(LOG_FILE|LOG_COUT);
Array<AsyncWork<Vector<int>>> workers;
Vector<int> results;
for(int i = 0; i < 10; i++) {
workers.Add() = Async([=]{
Sleep(Random(200));
Vector<int> v;
for(int j = i * 10; j < i * 10 + 10; j++)
v.Add(j);
return pick(v);
});
}
while(!workers.IsEmpty())
for(int i = 0; i < workers.GetCount(); i++) {
auto& w = workers[i];
if(w.IsFinished()) { // Non blocking check. It will not wait.
// No locking required, since we dont use a shared variable...
auto iota = w.Pick(); // Same as Get() but picks ("moves") the resulting vector.
RDUMP(iota);
results.Append(iota);
Sort(results);
RLOG("Sorted partial results: " << results);
workers.Remove(i);
break;
}
}
RLOG("Final results: " << results);
}
Hopefully, this example can demonstrate the basic usage.
If you want a "real" example, check the SshBasics/SftpMT.cpp example here: https://www.ultimatepp.org/reference$SshBasics$en-us.html
IF you have more questions and/or need more help, let me know.
Best regards,
Oblivion
Github page: https://github.com/ismail-yilmaz
upp-components: https://github.com/ismail-yilmaz/upp-components
Bobcat the terminal emulator: https://github.com/ismail-yilmaz/Bobcat
[Updated on: Sat, 18 July 2020 15:59] Report message to a moderator
|
|
|
|
|
AsyncWork, IsFinished() may not be working properly
By: Oblivion on Wed, 28 March 2018 19:17
|
|
|
Re: AsyncWork, IsFinished() may not be working properly
By: mirek on Sat, 31 March 2018 07:49
|
|
|
Re: AsyncWork, IsFinished() may not be working properly
By: Oblivion on Sat, 31 March 2018 12:51
|
|
|
Re: AsyncWork, IsFinished() may not be working properly
By: JeyCi on Fri, 17 July 2020 12:48
|
|
|
Re: AsyncWork, IsFinished() may not be working properly
By: JeyCi on Sat, 18 July 2020 07:51
|
|
|
Re: AsyncWork, IsFinished() may not be working properly
|
|
|
Re: AsyncWork, IsFinished() may not be working properly
By: JeyCi on Sat, 18 July 2020 14:21
|
|
|
Re: AsyncWork, IsFinished() may not be working properly
|
|
|
Re: AsyncWork, IsFinished() may not be working properly
By: JeyCi on Sat, 18 July 2020 16:44
|
|
|
Re: AsyncWork, IsFinished() may not be working properly
By: JeyCi on Sat, 18 July 2020 17:06
|
|
|
Re: AsyncWork, IsFinished() may not be working properly
|
|
|
Re: AsyncWork, IsFinished() may not be working properly
By: JeyCi on Sat, 18 July 2020 18:34
|
|
|
Re: AsyncWork, IsFinished() may not be working properly
|
|
|
Re: AsyncWork, IsFinished() may not be working properly
By: JeyCi on Sat, 18 July 2020 21:02
|
Goto Forum:
Current Time: Sun Apr 28 09:50:46 CEST 2024
Total time taken to generate the page: 0.04695 seconds
|