GtkD code review - How to update a progressbar using data sharing concurrency

Ali Çehreli acehreli at yahoo.com
Sun Jun 21 09:16:06 UTC 2020


On 6/20/20 9:30 AM, adnan338 wrote:

 > Hello, I need a code review on my strategy

I don't know gtkd so I did not compile the code and I did not review the 
code very carefully.

However, I don't think you need to 'synchronized' the whole parallel 
loop. Since there is only one thread that executes start(), that 
synchronized cannot have any effect at all. What you want to synchronize 
is the mutating access to 'completed' by the threads that parallel() 
starts automatically. So, move 'synchronized' just around that expression:

// REMOVE this one:
// synchronized
// {

   foreach (_; downloader.links.parallel())
   {
       Thread.sleep(uniform(0, 6, rnd).seconds());

       // ADD this one:
       synchronized {
         ++cast() downloader.completed;
       }
   }

// }

Ali



More information about the Digitalmars-d-learn mailing list