|
|
Home » U++ Library support » U++ MT-multithreading and servers » How to use CoWork and Progress together?
How to use CoWork and Progress together? [message #53511] |
Tue, 07 April 2020 14:58 |
Tom1
Messages: 1242 Registered: March 2007
|
Senior Contributor |
|
|
Hi,
I've been trying to make CoWork and Progress work nicely together, but I just can't figure out what have I done wrong:
Progress pg;
pg.Title("A parallel processing job");
pg.SetText("Working on many items at once...");
pg.Set(0, 100);
Atomic done=0;
CoWork co;
co * [&] {
for(int i = co.Next(); i < 100 ; i = co.Next()) {
Sleep(100); // Doing something...
// OK, now it is done, update progress:
GuiLock _;
pg.SetPos(AtomicInc(done));
}
};
co.Finish();
Any suggestions how to make it work as it should? Now it just freezes at or near the end...
Best regards,
Tom
|
|
|
Re: How to use CoWork and Progress together? [message #53514 is a reply to message #53511] |
Tue, 07 April 2020 16:39 |
Tom1
Messages: 1242 Registered: March 2007
|
Senior Contributor |
|
|
Hi,
I finally found the solution:
Progress pg;
pg.Title("A parallel processing job");
pg.SetText("Working on many items at once...");
pg.Set(0, 100);
Atomic done=0;
Atomic stop=0;
CoWork co;
for(int t=0;t<co.GetPoolSize();t++){
co & [&] {
for(int i = AtomicInc(done); i<100;i = AtomicInc(done)){
Sleep(1000); // Doing something...
if(stop) break;
}
};
}
while(!co.IsFinished()){
if(pg.Canceled()){
AtomicInc(stop);
break;
}
pg.SetPos(done);
Sleep(100);
}
co.Finish();
So, in the end I did have to give up the CoWork::Loop (operator *) and do approximately the same externally to avoid internal co.Finish(); Now the GUI is accessed entirely in the main thread and there are no more issues.
Best regards,
Tom
|
|
|
|
|
|
Re: How to use CoWork and Progress together? [message #53554 is a reply to message #53540] |
Sat, 11 April 2020 11:14 |
Tom1
Messages: 1242 Registered: March 2007
|
Senior Contributor |
|
|
Hi Mirek,
This is a nice and stylish improvement!
However, there is still something to fix as using this through a menu leaves the app frozen at or near the completion of parallel processing. Here's a testcase based on the reference example:
#include <CtrlLib/CtrlLib.h>
using namespace Upp;
class Test: public TopWindow{
public:
typedef Test CLASSNAME;
MenuBar menu;
void Worker(){
Progress pi("Working on many items at once...", 100);
std::atomic<int> ii(0);
CoDo([&] {
for(int i = ii++; i < 100; i = ii++) {
for(int q = 0; q < 1000; q++) {
if(pi.Canceled()) // ideally call Canceled every 1-10ms
return;
Sleep(1); // work simulation
}
pi.Step();
}
});
}
void MainMenu(Bar &bar){
bar.Add("Process...", THISFN(Worker));
}
Test(){
AddFrame(menu);
menu.Set([=](Bar& bar) { MainMenu(bar); });
}
};
GUI_APP_MAIN
{
Test test;
test.Run();
}
Thanks and best regards,
Tom
|
|
|
|
|
|
|
|
Goto Forum:
Current Time: Fri Sep 20 16:43:26 CEST 2024
Total time taken to generate the page: 0.03925 seconds
|
|
|