From 38cf5d93647bf5ffb5fe3e17447eba0c157bb305 Mon Sep 17 00:00:00 2001
From: "Erik M. Bray" <erik.bray@lri.fr>
Date: Sun, 28 Oct 2018 21:16:52 +0000
Subject: [PATCH] ensure that threading has been initialized in the first place
 before calling openblas_set_num_threads

---
 driver/others/blas_server.c       | 5 +++++
 driver/others/blas_server_win32.c | 7 ++++++-
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/driver/others/blas_server.c b/driver/others/blas_server.c
index 6a25e2d07..e5db1804f 100644
--- a/driver/others/blas_server.c
+++ b/driver/others/blas_server.c
@@ -850,6 +850,11 @@ void goto_set_num_threads(int num_threads) {
 
   long i;
 
+#ifdef SMP_SERVER
+  // Handle lazy re-init of the thread-pool after a POSIX fork
+  if (unlikely(blas_server_avail == 0)) blas_thread_init();
+#endif
+
   if (num_threads < 1) num_threads = blas_num_threads;
 
 #ifndef NO_AFFINITY
diff --git a/driver/others/blas_server_win32.c b/driver/others/blas_server_win32.c
index 02a25ac39..bae344c59 100644
--- a/driver/others/blas_server_win32.c
+++ b/driver/others/blas_server_win32.c
@@ -478,7 +478,12 @@ int BLASFUNC(blas_thread_shutdown)(void){
 
 void goto_set_num_threads(int num_threads)
 {
-	 long i;
+	long i;
+
+#if defined(SMP_SERVER) && defined(OS_CYGWIN_NT)
+	// Handle lazy re-init of the thread-pool after a POSIX fork
+	if (unlikely(blas_server_avail == 0)) blas_thread_init();
+#endif
 
 	if (num_threads < 1) num_threads = blas_cpu_number;
 
