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
Re: AsyncWork, IsFinished() may not be working properly [message #54431 is a reply to message #54430] Sat, 18 July 2020 15:31 Go to previous messageGo to previous message
Oblivion is currently offline  Oblivion
Messages: 1093
Registered: August 2007
Senior Contributor
Happy to help Smile

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


[Updated on: Sat, 18 July 2020 15:59]

Report message to a moderator

 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Skylark session verification
Next Topic: How do I create a loop for a window to react to volatile changes to a global variable?
Goto Forum:
  


Current Time: Sun Apr 28 09:50:46 CEST 2024

Total time taken to generate the page: 0.04695 seconds