[gnome-sudoku/multithread_printing] Make all threads run simultaneously



commit 28cd35d686427409530b2b0c52157631862fc91f
Author: Parin Porecha <parinporecha gmail com>
Date:   Fri Aug 15 18:06:55 2014 +0200

    Make all threads run simultaneously
    
    Also, change ThreadFunc from int to void*
    - Make print dialog insensitive while generation is in progress

 lib/sudoku-generator.vala |   17 +++++++++--------
 src/sudoku-printer.vala   |    2 ++
 2 files changed, 11 insertions(+), 8 deletions(-)
---
diff --git a/lib/sudoku-generator.vala b/lib/sudoku-generator.vala
index c2b1c47..233b2d2 100644
--- a/lib/sudoku-generator.vala
+++ b/lib/sudoku-generator.vala
@@ -28,6 +28,7 @@ public class SudokuGenerator : Object
     {
         var boards_list = new ArrayList<SudokuBoard> ();
         var boards = new SudokuBoard[nboards];
+        Thread<void*> thread[8];
 
 //        var sysinfo = GTop.glibtop_get_sysinfo ();
 //        stdout.printf ("ncpus = %d\n", (int) sysinfo.ncpu);
@@ -44,14 +45,14 @@ public class SudokuGenerator : Object
             if (i > (nthreads - remainder - 1))
                 nsudokus_per_thread = base_nsudokus_each + 1;
             var gen_thread = new GeneratorThread (nsudokus_per_thread, category, ref boards_list, 
generate_boards_async.callback, i);
-            Thread<int> thread = new Thread<int> ("Generator thread", gen_thread.run);
+            thread[i] = new Thread<void*> ("Generator thread", gen_thread.run);
+        }
 
-            // Relinquish the CPU, so that the generated thread can run
+        // Relinquish the CPU, so that the generated threads can run
+        for (var i = 0; i < nthreads; i++)
+        {
             yield;
-
-            stdout.printf ("waiting for #%d to join\n", i);
-            var result = thread.join ();
-            stdout.printf ("Thread #%d exited\n", result);
+            thread[i].join ();
         }
 
         stdout.printf ("boards list size = %d\n", boards_list.size);
@@ -96,13 +97,13 @@ public class GeneratorThread : Object
         this.callback = callback;
     }
 
-    public int run ()
+    public void* run ()
     {
         stdout.printf ("generating %d puzzles\n", nsudokus);
         for (var i = 0; i < nsudokus; i++)
             boards_list.add (SudokuGenerator.generate_board (level));
 
         Idle.add((owned) callback);
-        return id;
+        return null;
     }
 }
diff --git a/src/sudoku-printer.vala b/src/sudoku-printer.vala
index 89b6fab..a874031 100644
--- a/src/sudoku-printer.vala
+++ b/src/sudoku-printer.vala
@@ -322,6 +322,7 @@ public class GamePrinter: GLib.Object
         spinner.active = true;
         spinner.show ();
         spinner.start ();
+        dialog.sensitive = false;
 
         SudokuGenerator.generate_boards_async.begin(nsudokus, level, (obj, res) => {
             try {
@@ -329,6 +330,7 @@ public class GamePrinter: GLib.Object
 
                 spinner.stop ();
                 spinner.hide ();
+                dialog.sensitive = true;
 
                 SudokuPrinter printer = new SudokuPrinter (boards, ref window);
                 PrintOperationResult result = printer.print_sudoku ();


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]