Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 17 additions & 20 deletions tutorials/performance/using_multiple_threads.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
:article_outdated: True

.. _doc_using_multiple_threads:

Using multiple threads
Expand Down Expand Up @@ -29,23 +27,22 @@ To create a thread, use the following code:
.. tabs::
.. code-tab:: gdscript GDScript

var thread
var thread: Thread

# The thread will start here.
func _ready():
thread = Thread.new()
# Third argument is optional userdata, it can be any variable.
thread.start(self, "_thread_function", "Wafflecopter")
# You can bind multiple arguments to a function Callable.
thread.start(_thread_function.bind("Wafflecopter"))


# Run here and exit.
# The argument is the userdata passed from start().
# If no argument was passed, this one still needs to
# be here and it will be null.
# The argument is the bound data passed from start().
func _thread_function(userdata):
# Print the userdata ("Wafflecopter")
print("I'm a thread! Userdata is: ", userdata)


# Thread must be disposed (or "joined"), for portability.
func _exit_tree():
thread.wait_to_finish()
Expand Down Expand Up @@ -86,16 +83,16 @@ Here is an example of using a Mutex:
.. tabs::
.. code-tab:: gdscript GDScript

var counter = 0
var mutex
var thread
var counter := 0
var mutex: Mutex
var thread: Thread


# The thread will start here.
func _ready():
mutex = Mutex.new()
thread = Thread.new()
thread.start(self, "_thread_function")
thread.start(_thread_function)

# Increase value, protect it with Mutex.
mutex.lock()
Expand All @@ -104,7 +101,7 @@ Here is an example of using a Mutex:


# Increment the value from the thread, too.
func _thread_function(userdata):
func _thread_function():
mutex.lock()
counter += 1
mutex.unlock()
Expand All @@ -131,11 +128,11 @@ ready to be processed:
.. tabs::
.. code-tab:: gdscript GDScript

var counter = 0
var mutex
var semaphore
var thread
var exit_thread = false
var counter := 0
var mutex: Mutex
var semaphore: Semaphore
var thread: Thread
var exit_thread := false


# The thread will start here.
Expand All @@ -145,10 +142,10 @@ ready to be processed:
exit_thread = false

thread = Thread.new()
thread.start(self, "_thread_function")
thread.start(_thread_function)


func _thread_function(userdata):
func _thread_function():
while true:
semaphore.wait() # Wait until posted.

Expand Down