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 » Newbie corner » The right way to use CoDo with GuiLock? (The app freeze when trying to update the GUI while CoDo)
Re: The right way to use CoDo with GuiLock? [message #58545 is a reply to message #58544] Sat, 11 June 2022 14:43 Go to previous messageGo to previous message
Oblivion is currently offline  Oblivion
Messages: 1206
Registered: August 2007
Senior Contributor
Hello Mountacir,

There can be several approaches here, but I'd suggest these two:

void CoTest::CoForTest1()
{
	list.Clear();
	Vector<int> v;
	CoFor(100, [=, &v](int i) {
		CoWork::FinLock();
		v.Add(i);
	});
	std::for_each(v.begin(), v.end(), [=](auto n) { list.Add(n); });
}

void CoTest::CoForTest2()
{
	list.Clear();
	GuiUnlock __; // Unlock the global Gui Mutex so it can be acquired by the workers...
	CoFor(100, [=](int i) {
		GuiLock __; // Acquire ...
		list.Add(i);
	});
}



CoFor is a convenience function that does what you just do in your example: iteration.

Both CoDo and CoFor are blocking functions. They will wait for the workers to join.

So, first you will need to release the gui lock of main gui thread, using the GuiUnlock class.

IMO, however, CoForTest1() would be a better and safer way, as the CoForTest2 will start blocking the GUI when the item count is high (try with N=10000 and see the difference in the results).

P.s:

This is your example, done in the right way:

void CoTest::CoDoTest()
{
	list.Clear();
	std::atomic<int> ii(0);
	GuiUnlock __;
	CoDo([=, &ii] {
		GuiLock __;
		for(int i =ii++; i < 100 ; i=ii++) {
			list.Add(i);
		
		}
	});
}




Best regards,
Oblivion


[Updated on: Sat, 11 June 2022 14:52]

Report message to a moderator

 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Different File List
Next Topic: SVG for tool icon
Goto Forum:
  


Current Time: Sat May 10 23:44:28 CEST 2025

Total time taken to generate the page: 0.00338 seconds