@@ -161,10 +161,16 @@ s_zsys_vprintf_hint (int hint, const char *format, va_list argptr);
161
161
// mutex for pthread_once to run the init function only once in a process
162
162
static pthread_once_t init_all_mutex_var = PTHREAD_ONCE_INIT ;
163
163
164
+ // handler to destroy s_mutex
165
+ static void zsys_destroy_mutex () {
166
+ ZMUTEX_DESTROY (s_mutex );
167
+ }
168
+
164
169
// handler to initialize mutexes one time in multi threaded env
165
170
static void zsys_initialize_mutex () {
166
171
ZMUTEX_INIT (s_mutex );
167
172
ZMUTEX_INIT (s_init_mutex );
173
+ atexit (zsys_destroy_mutex );
168
174
}
169
175
170
176
// handler to detect fork condition and cleanup the stale context inherited from parent process
@@ -175,6 +181,13 @@ static void zsys_pthread_at_fork_handler(void) {
175
181
// call cleanup
176
182
zsys_cleanup ();
177
183
}
184
+
185
+ // mutex for pthread_once to register the atexit function only once in a process
186
+ static pthread_once_t register_atexit_shutdown = PTHREAD_ONCE_INIT ;
187
+ // handler to register the atexit function one time in multi threaded env
188
+ static void zsys_register_atexit_shutdown (void ) {
189
+ atexit (zsys_shutdown );
190
+ }
178
191
#endif
179
192
180
193
void *
@@ -279,7 +292,7 @@ zsys_init (void)
279
292
s_initialized = true;
280
293
281
294
#if defined (__UNIX__ )
282
- atexit ( zsys_shutdown );
295
+ pthread_once ( & register_atexit_shutdown , zsys_register_atexit_shutdown );
283
296
pthread_atfork (NULL , NULL , & zsys_pthread_at_fork_handler );
284
297
//don't hold the lock because some of the function will call zsys_init again
285
298
ZMUTEX_UNLOCK (s_init_mutex );
@@ -407,7 +420,9 @@ zsys_shutdown (void)
407
420
else
408
421
zsys_error ("dangling sockets: cannot terminate ZMQ safely" );
409
422
423
+ #if !defined (__UNIX__ )
410
424
ZMUTEX_DESTROY (s_mutex );
425
+ #endif
411
426
412
427
// Free dynamically allocated properties
413
428
freen (s_interface );
0 commit comments