Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ MT-multithreading and servers » AsyncWork, IsFinished() may not be working properly
AsyncWork, IsFinished() may not be working properly [message #49671] Wed, 28 March 2018 19:17 Go to next message
Oblivion is currently offline  Oblivion
Messages: 536
Registered: August 2007
Location: Turkey
Contributor
Hello,

Either I am doing something wrong, or AsyncWork/CoWork's IsFinished() method is not working properly (Tested on latest Upp/GCC/Linux kernel).

It always returns true;


Here is a simple test code to show the problem:

#include <Core/Core.h>

using namespace Upp;

CONSOLE_APP_MAIN
{
	StdLogSetup(LOG_FILE|LOG_COUT);
	
	Array<AsyncWork<void>> workers;
	
	for(int i = 0; i < 4; i++) {
		workers.Add() = Async([=]{ LOG("Started, worker #" << i); for(;;); });
	}
	Sleep(5000);
	
//	while(!workers.IsEmpty()) 
		for(int i = 0; i < workers.GetCount(); i++) {
			auto& w = workers[i];
			if(w.IsFinished())
				LOG("Stopped, worker #" << i); // <<- Called, when it shouldn't.
		}
}



In the above test code, workers are supposed to run forever. So IsFinished should (?) return false. But when I check them with IsFinished() method, I get positive results every time.

Am I doing something wrong?

Here is the log:


CoWork constructed 7f86caf82060
Do0, looper: false, previous todo: 0
Pool::InitThreads: 8
CoWork thread #0 started
CoWork thread #1 started
CoWork thread #2 started
CoWork thread #3 started
CoWork thread #4 started
CoWork thread #5 started
CoWork thread #6 started
CoWork thread #7 started
#1 Waiting for job
Adding job
Releasing thread waiting for job, waiting threads: 1
#1 Waiting ended
#1 Job acquired
DoJob (CoWork 7f86caf82060)
Started worker #0
#4 Waiting for job
CoWork constructed 7f86caf82140
Do0, looper: false, previous todo: 0
#5 Waiting for job
#0 Waiting for job
#3 Waiting for job
#6 Waiting for job
#7 Waiting for job
#2 Waiting for job
Adding job
Releasing thread waiting for job, waiting threads: 7
CoWork constructed 7f86caf82220
Do0, looper: false, previous todo: 0
#4 Waiting ended
#4 Job acquired
DoJob (CoWork 7f86caf82140)
Started worker #1
Adding job
Releasing thread waiting for job, waiting threads: 6
CoWork constructed 7f86caf82300
Do0, looper: false, previous todo: 0
Adding job
Releasing thread waiting for job, waiting threads: 6
#0 Waiting ended
#0 Job acquired
DoJob (CoWork 7f86caf82300)
Started worker #3
#5 Waiting ended
#5 Job acquired
DoJob (CoWork 7f86caf82220)
Started worker #2
Stopped worker #0
Stopped worker #1
Stopped worker #2
Stopped worker #3
CoWork Cancel0
WaitForFinish (CoWork 7f86caf82060)
//...


Best regards,
Oblivion

[Updated on: Wed, 28 March 2018 19:17]

Report message to a moderator

Re: AsyncWork, IsFinished() may not be working properly [message #49677 is a reply to message #49671] Sat, 31 March 2018 07:49 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 11715
Registered: November 2005
Ultimate Member
Hopefully fixed. (CoWork::IsFinished bug).

Final testcase:

#include <Core/Core.h>

using namespace Upp;

CONSOLE_APP_MAIN
{
	StdLogSetup(LOG_FILE|LOG_COUT);
	
	Array<AsyncWork<void>> workers;
	
	for(int i = 0; i < 4; i++) {
		workers.Add() = Async([=]{ LOG("Started, worker #" << i); Sleep(500); });
	}
	
	while(!workers.IsEmpty())
		for(int i = 0; i < workers.GetCount(); i++) {
			auto& w = workers[i];
			if(w.IsFinished()) {
				LOG("Stopped, worker #" << i); // <<- Called, when it shouldn't.
				workers.Remove(i);
				break;
			}
		}
}
Re: AsyncWork, IsFinished() may not be working properly [message #49678 is a reply to message #49677] Sat, 31 March 2018 12:51 Go to previous message
Oblivion is currently offline  Oblivion
Messages: 536
Registered: August 2007
Location: Turkey
Contributor
Hello Mirek,

Thanks! Now it's working as expected.

Best regards,
Oblivion.
Previous Topic: HttpRequest ignores server errors
Next Topic: how to broadcast in AsyncWebSocket?
Goto Forum:
  


Current Time: Sat Feb 23 20:44:20 CET 2019

Total time taken to generate the page: 0.00683 seconds