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 #54429 is a reply to message #54428] |
Sat, 18 July 2020 09:59 |
Oblivion
Messages: 1093 Registered: August 2007
|
Senior Contributor |
|
|
Hello JeyCi,
And welcome to the U++ forums!
Note that when the worker is removed from the array in the first example, the item count is also decreased by 1.
This obviously invalidates the indices, so the code breaks the for loop and starts it all over again (from 0).
Hence the "two or more zeros" in the log output. It might give the impression of race condition, but it is not. (If that's the problem.)
(The reason is that the first example was about a problem in IsFinished(). It was always returning true.
The example did not rely on those indices for its purpose, so I didn't care to make it more correct...)
Please try this one instead:
CONSOLE_APP_MAIN
{
StdLogSetup(LOG_FILE|LOG_COUT);
Array<AsyncWork<int>> workers;
for(int i = 0; i < 4; i++) {
workers.Add() = Async([=]{ LOG("Started, worker #" << i); Sleep(500); return i; });
}
while(!workers.IsEmpty())
for(int i = 0; i < workers.GetCount(); i++) {
auto& w = workers[i];
if(w.IsFinished()) {
LOG("Stopped, worker #" << w.Get());
workers.Remove(i);
break;
}
}
}
Quote:
I found below code seems working good
Yes, that works, because, unlike the "non-blokcking" first example, this one has a "blocking" loop:
for (auto &fut : workers) {
LOG("Stopped, worker #" << fut.Get());
}
The Get() method will wait the worker to finish its job unless it is alreay finished. So it is "blocking".
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 10:29] 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 20:31:22 CEST 2024
Total time taken to generate the page: 0.05714 seconds
|