@@ -148,26 +148,26 @@ impl Default for InitializationOptions {
148148/// This initializes the core with the given [`InitializationOptions`].
149149pub fn init_with_opts ( options : InitializationOptions ) -> Result < ( ) , InitializationError > {
150150 // If we are the main thread that means there is no main thread, we should register a main thread handler.
151- if options. register_main_thread_handler
152- && is_main_thread ( )
153- && MAIN_THREAD_HANDLE . lock ( ) . unwrap ( ) . is_none ( )
154- {
155- let ( sender , receiver ) = std :: sync :: mpsc :: channel ( ) ;
156- let main_thread = HeadlessMainThreadSender :: new ( sender ) ;
157-
158- // This thread will act as our main thread.
159- let main_thread_handle = std :: thread :: Builder :: new ( )
160- . name ( "HeadlessMainThread" . to_string ( ) )
161- . spawn ( move || {
162- // We must register the main thread within said thread.
163- main_thread . register ( ) ;
164- while let Ok ( action ) = receiver . recv ( ) {
165- action . execute ( ) ;
166- }
167- } ) ? ;
168-
169- // Set the static MAIN_THREAD_HANDLER so that we can close the thread on shutdown.
170- * MAIN_THREAD_HANDLE . lock ( ) . unwrap ( ) = Some ( main_thread_handle ) ;
151+ if options. register_main_thread_handler && is_main_thread ( ) {
152+ let mut main_thread_handle = MAIN_THREAD_HANDLE . lock ( ) . unwrap ( ) ;
153+ if main_thread_handle . is_none ( ) {
154+ let ( sender , receiver ) = std :: sync :: mpsc :: channel ( ) ;
155+ let main_thread = HeadlessMainThreadSender :: new ( sender ) ;
156+
157+ // This thread will act as our main thread.
158+ let join_handle = std :: thread:: Builder :: new ( )
159+ . name ( "HeadlessMainThread" . to_string ( ) )
160+ . spawn ( move || {
161+ // We must register the main thread within said thread.
162+ main_thread . register ( ) ;
163+ while let Ok ( action ) = receiver . recv ( ) {
164+ action . execute ( ) ;
165+ }
166+ } ) ? ;
167+
168+ // Set the static MAIN_THREAD_HANDLER so that we can close the thread on shutdown.
169+ * main_thread_handle = Some ( join_handle ) ;
170+ }
171171 }
172172
173173 match crate :: product ( ) . as_str ( ) {
0 commit comments