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 » CoWork::FinLock
CoWork::FinLock [message #45810] Sat, 09 January 2016 20:24 Go to previous message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Do not ask me why, but recently I was having fun with parallel programming (I looks like it will be unintended addition for the next release... Smile.

In the process, while optimizing everything around CoWork, I got an interesting idea, CoWork::FinLock. It is intended to lock storing partial results into shared target at the end of scheduled routine. The idea is that CoWork has to lock some mutex anyway after the routine ends, so it is possible to 'prelock' this internal mutex and join both critical sections.

Usage looks something like this:

template <typename I, typename Lambda>
void CoLoop(I begin, I end, const Lambda& lambda)
{
	size_t chunk = max(size_t((end - begin) / CPU_Cores()), (size_t)1);
	CoWork co;
	while(begin < end) {
		co & [=] {
			lambda(begin, begin + min(chunk, size_t(end - begin)));
		};
		begin += chunk;
	}
}

template <class I, class Better>
I CoBest(I begin, I end, const Better& better)
{
	I best = begin++;
	CoLoop(begin, end,
		[=, &best](I i, I e) {
			I b = i++;
			while(i < e) {
				if(better(*i, *b))
					b = i;
				i++;
			}
			CoWork::FinLock(); // better is shared across threads, needs a lock!
			if(better(*b, *best))
				best = b;
		}
	);
	return best;
}


(CoBest is intended to lookup e.g. minimum or maximum, 'best' value).
 
Read Message
Read Message
Read Message
Read Message
Previous Topic: Happy New Year 2016
Next Topic: TheIDE - Python syntax and indention support
Goto Forum:
  


Current Time: Sun May 05 15:06:36 CEST 2024

Total time taken to generate the page: 0.02669 seconds