Browse Source

Add exit condition to prevent thread from hanging in resource_queue.gd (#5833)

Co-authored-by: Hugo Locurcio <[email protected]>
Telmo "Trooper 3 years ago
parent
commit
24bc3db479
1 changed files with 25 additions and 4 deletions
  1. 25 4
      tutorials/io/files/resource_queue.gd

+ 25 - 4
tutorials/io/files/resource_queue.gd

@@ -1,6 +1,9 @@
+extends Node
+
 var thread
 var mutex
-var sem
+var semaphore
+var exit_thread = false
 
 var time_max = 100 # Milliseconds.
 
@@ -16,11 +19,11 @@ func _unlock(_caller):
 
 
 func _post(_caller):
-	sem.post()
+	semaphore.post()
 
 
 func _wait(_caller):
-	sem.wait()
+	semaphore.wait()
 
 
 func queue_resource(path, p_in_front = false):
@@ -135,11 +138,29 @@ func thread_process():
 
 func thread_func(_u):
 	while true:
+		mutex.lock()
+		var should_exit = exit_thread # Protect with Mutex.
+		mutex.unlock()
+
+		if should_exit:
+			break
 		thread_process()
 
 
 func start():
 	mutex = Mutex.new()
-	sem = Semaphore.new()
+	semaphore = Semaphore.new()
 	thread = Thread.new()
 	thread.start(self, "thread_func", 0)
+
+# Triggered by calling "get_tree().quit()".
+func _exit_tree():
+	mutex.lock()
+	exit_thread = true # Protect with Mutex.
+	mutex.unlock()
+
+	# Unblock by posting.
+	semaphore.post()
+
+	# Wait until it exits.
+	thread.wait_to_finish()