fixes #935 nn_term will crash if no nn_socket is ever created
diff --git a/src/core/global.c b/src/core/global.c
index 9fc55ea..6fbf003 100644
--- a/src/core/global.c
+++ b/src/core/global.c
@@ -168,6 +168,7 @@
 
     int print_errors;
 
+    int inited;
     nn_mutex_t lock;
     nn_condvar_t cond;
 };
@@ -306,6 +307,10 @@
 {
     int i;
 
+    if (!self.inited) {
+        return;
+    }
+
     nn_mutex_lock (&self.lock);
     self.flags |= NN_CTX_FLAG_TERMING;
     nn_mutex_unlock (&self.lock);
@@ -323,8 +328,18 @@
     nn_mutex_unlock (&self.lock);
 }
 
+static void nn_lib_init(void)
+{
+    /*  This function is executed once to initialize global locks. */
+    nn_mutex_init (&self.lock);
+    nn_condvar_init (&self.cond);
+    self.inited = 1;
+}
+
 void nn_init (void)
 {
+    nn_do_once (&once, nn_lib_init);
+
     nn_mutex_lock (&self.lock);
     /*  Wait for any in progress term to complete. */
     while (self.flags & NN_CTX_FLAG_TERMING) {
@@ -454,13 +469,6 @@
     return -EINVAL;
 }
 
-static void nn_lib_init(void)
-{
-    /*  This function is executed once to initialize global locks. */
-    nn_mutex_init (&self.lock);
-    nn_condvar_init (&self.cond);
-}
-
 int nn_socket (int domain, int protocol)
 {
     int rc;