[gnome-sudoku/multithread_printing] Move generating sudokus to separate threads



commit cf653bcd901cccbae253443abf8d6f708dd2a7c0
Author: Parin Porecha <parinporecha gmail com>
Date:   Wed Aug 13 17:35:32 2014 +0200

    Move generating sudokus to separate threads

 configure.ac            |    1 +
 src/Makefile.am         |    1 +
 src/sudoku-printer.vala |   57 +++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 57 insertions(+), 2 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 96073d2..55a7ab5 100644
--- a/configure.ac
+++ b/configure.ac
@@ -32,6 +32,7 @@ PKG_CHECK_MODULES(GNOME_SUDOKU, [
   gio-2.0 >= $GLIB_REQUIRED
   gtk+-3.0 >= $GTK_REQUIRED
   gee-0.8
+  libgtop-2.0
 ])
 
 PKG_CHECK_MODULES(LIBSUDOKU, [
diff --git a/src/Makefile.am b/src/Makefile.am
index f711496..b3c7087 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -31,6 +31,7 @@ gnome_sudoku_VALAFLAGS = \
        --pkg gio-2.0 \
        --pkg gtk+-3.0 \
        --pkg gee-0.8 \
+       --target-glib=2.32      \
        $(top_builddir)/lib/libsudoku.vapi
 
 gnome-sudoku-resources.c: gnome-sudoku.gresource.xml $(shell $(GLIB_COMPILE_RESOURCES) 
--generate-dependencies gnome-sudoku.gresource.xml)
diff --git a/src/sudoku-printer.vala b/src/sudoku-printer.vala
index 5ccd2f3..b80dea9 100644
--- a/src/sudoku-printer.vala
+++ b/src/sudoku-printer.vala
@@ -3,6 +3,9 @@
 using Gtk;
 using Gee;
 using Gdk;
+//using GLibtop;
+
+ArrayList<SudokuBoard> boards_list;
 
 public class SudokuPrinter : GLib.Object {
 
@@ -295,6 +298,7 @@ public class GamePrinter: GLib.Object
             return;
         }
 
+        boards_list = new ArrayList<SudokuBoard> ();
         var nsudokus = (int) nsudokus_button.get_adjustment ().get_value ();
         DifficultyCategory level;
         var boards = new SudokuBoard[nsudokus];
@@ -312,8 +316,34 @@ public class GamePrinter: GLib.Object
 
         settings.set_enum (DIFFICULTY_KEY_NAME, level);
 
-        for (var i = 0; i < nsudokus; i++)
-            boards[i] = SudokuGenerator.generate_board (level);
+        if (Thread.supported () == false)
+        {
+            for (var i = 0; i < nsudokus; i++)
+                boards[i] = SudokuGenerator.generate_board (level);
+        }
+        else
+        {
+//            var sysinfo = GLibtop.glibtop_sysinfo.get_sysinfo ();
+//            stdout.printf ("ncpus = %d\n", sysinfo.ncpu);
+
+            var ncpu = 4;
+            var base_nsudokus_each = nsudokus / ncpu;
+            var remainder = nsudokus % ncpu;
+            var nsudokus_per_thread = base_nsudokus_each;
+
+            for (var i = 0; i < ncpu; i++)
+            {
+                if (i > (ncpu - remainder - 1))
+                    nsudokus_per_thread = base_nsudokus_each + 1;
+                var gen_thread = new GeneratorThread (nsudokus_per_thread, level, i);
+                Thread<int> thread = new Thread<int> ("Generator thread", gen_thread.run);
+                var result = thread.join ();
+                stdout.printf ("Thread #%d exited\n", result);
+            }
+
+            for (var i = 0; i < boards_list.size; i++)
+                boards[i] = boards_list[i];
+        }
 
         SudokuPrinter printer = new SudokuPrinter (boards, ref window);
 
@@ -324,6 +354,7 @@ public class GamePrinter: GLib.Object
             foreach (SudokuBoard board in boards)
                 saver.add_game_to_finished (new SudokuGame (board));
         }
+        boards_list = null;
     }
 
     public void run_dialog ()
@@ -332,3 +363,25 @@ public class GamePrinter: GLib.Object
     }
 
 }
+
+public class GeneratorThread : Object
+{
+    private int nsudokus;
+    private DifficultyCategory level;
+    private int id;
+
+    public GeneratorThread (int nsudokus, DifficultyCategory level, int id) {
+        this.nsudokus = nsudokus;
+        this.level = level;
+        this.id = id;
+    }
+
+    public int run ()
+    {
+        stdout.printf ("generating %d puzzles\n", nsudokus);
+        for (var i = 0; i < nsudokus; i++)
+            boards_list.add (SudokuGenerator.generate_board (level));
+
+        return id;
+    }
+}


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