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   |
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
Github page: https://github.com/ismail-yilmaz
upp-components: https://github.com/ismail-yilmaz/upp-components
Bobcat the terminal emulator: https://github.com/ismail-yilmaz/Bobcat
[Updated on: Sat, 11 June 2022 14:52] Report message to a moderator
|
|
|
Goto Forum:
Current Time: Sat May 10 23:44:28 CEST 2025
Total time taken to generate the page: 0.00338 seconds
|