[gnome-sudoku/multithread_printing] Call nproc to find out number of processors



commit 428c9718031352135e74952e1cd73ce5c21d1336
Author: Parin Porecha <parinporecha gmail com>
Date:   Mon Aug 18 18:37:49 2014 +0200

    Call nproc to find out number of processors

 lib/sudoku-generator.vala |   20 +++++++++++++++++---
 1 files changed, 17 insertions(+), 3 deletions(-)
---
diff --git a/lib/sudoku-generator.vala b/lib/sudoku-generator.vala
index 233b2d2..f098fad 100644
--- a/lib/sudoku-generator.vala
+++ b/lib/sudoku-generator.vala
@@ -30,9 +30,7 @@ public class SudokuGenerator : Object
         var boards = new SudokuBoard[nboards];
         Thread<void*> thread[8];
 
-//        var sysinfo = GTop.glibtop_get_sysinfo ();
-//        stdout.printf ("ncpus = %d\n", (int) sysinfo.ncpu);
-        var ncpu = 4;
+        var ncpu = get_number_of_processors ();
         var nthreads = int.min (ncpu, nboards);
         var base_nsudokus_each = nboards / nthreads;
         var remainder = nboards % nthreads;
@@ -78,6 +76,22 @@ public class SudokuGenerator : Object
     {
         return QQwing.get_version ();
     }
+
+    private static int get_number_of_processors ()
+    {
+        int ncpu;
+        string nproc_stdout;
+
+        try {
+            Process.spawn_command_line_sync ("nproc", out nproc_stdout);
+            ncpu = int.parse (nproc_stdout);
+        } catch (SpawnError e) {
+            warning ("Call to nproc failed. Puzzles will be generated in a single thread");
+            ncpu = 1;
+        }
+
+        return ncpu;
+    }
 }
 
 public class GeneratorThread : Object


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