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 » Community » U++ community news and announcements » AsyncWork
AsyncWork [message #48857] Sat, 14 October 2017 11:34 Go to previous message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
AsyncWork is U++ take on std::future mechanism. The difference is that AsynWork is using CoWork thread pool as backend and, more importantly, allows for cancelation of job.

#include <Core/Core.h>

using namespace Upp;

CONSOLE_APP_MAIN
{
	StdLogSetup(LOG_FILE|LOG_COUT);
	
	auto a = Async([](int n) -> double {
		double f = 1;
		for(int i = 2; i <= n; i++)
			f *= i;
		return f;
	}, 100); // Schedules job to be executed by threadpool, returns AsyncWork for the return value and job control
	
	DUMP(a.Get()); // Makes sure job is finished (can execute it if it has not started yet), returns the result
	
	auto b = Async([] { throw "error"; });
	
	try {
		b.Get(); // exception is propagated
	}
	catch(...) {
		LOG("Exception has been caught");
	}
	
	
	auto c = Async([] {
		for(;;)
			if(CoWork::IsCanceled()) {
				LOG("Work was canceled");
				break;
			}
	});
	Sleep(100); // make it chance to start
	// c destructor cancels the work (can be explicitly canceled by Cancel method too)
}
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: ide: Repository sync and repo file version history refactored
Next Topic: plugin/lz4
Goto Forum:
  


Current Time: Sat Apr 27 21:38:37 CEST 2024

Total time taken to generate the page: 0.02591 seconds