Files
godot-docs-l10n/classes/fr/class_thread.rst
2025-12-19 14:34:07 +01:00

211 lines
10 KiB
ReStructuredText
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

:github_url: hide
.. _class_Thread:
Thread
======
**Hérite de:** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
Une unité d'exécution dans un processus.
.. rst-class:: classref-introduction-group
Description
-----------
A unit of execution in a process. Can run methods on :ref:`Object<class_Object>`\ s simultaneously. The use of synchronization via :ref:`Mutex<class_Mutex>` or :ref:`Semaphore<class_Semaphore>` is advised if working with shared objects.
\ **Warning:**\
To ensure proper cleanup without crashes or deadlocks, when a **Thread**'s reference count reaches zero and it is therefore destroyed, the following conditions must be met:
- It must not have any :ref:`Mutex<class_Mutex>` objects locked.
- It must not be waiting on any :ref:`Semaphore<class_Semaphore>` objects.
- :ref:`wait_to_finish()<class_Thread_method_wait_to_finish>` should have been called on it.
.. rst-class:: classref-introduction-group
Tutoriels
------------------
- :doc:`Utiliser plusieurs fils d'exécution <../tutorials/performance/using_multiple_threads>`
- :doc:`Les API sûres pour plusieurs fils d'exécution <../tutorials/performance/thread_safe_apis>`
- `Démo voxel 3D <https://godotengine.org/asset-library/asset/2755>`__
.. rst-class:: classref-reftable-group
Méthodes
----------------
.. table::
:widths: auto
+---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`String<class_String>` | :ref:`get_id<class_Thread_method_get_id>`\ (\ ) |const| |
+---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`bool<class_bool>` | :ref:`is_alive<class_Thread_method_is_alive>`\ (\ ) |const| |
+---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`bool<class_bool>` | :ref:`is_started<class_Thread_method_is_started>`\ (\ ) |const| |
+---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
| |void| | :ref:`set_thread_safety_checks_enabled<class_Thread_method_set_thread_safety_checks_enabled>`\ (\ enabled\: :ref:`bool<class_bool>`\ ) |static| |
+---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`Error<enum_@GlobalScope_Error>` | :ref:`start<class_Thread_method_start>`\ (\ callable\: :ref:`Callable<class_Callable>`, priority\: :ref:`Priority<enum_Thread_Priority>` = 1\ ) |
+---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`Variant<class_Variant>` | :ref:`wait_to_finish<class_Thread_method_wait_to_finish>`\ (\ ) |
+---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
.. rst-class:: classref-section-separator
----
.. rst-class:: classref-descriptions-group
Énumérations
------------------------
.. _enum_Thread_Priority:
.. rst-class:: classref-enumeration
enum **Priority**: :ref:`🔗<enum_Thread_Priority>`
.. _class_Thread_constant_PRIORITY_LOW:
.. rst-class:: classref-enumeration-constant
:ref:`Priority<enum_Thread_Priority>` **PRIORITY_LOW** = ``0``
Un fil d'exécution avec une priorité inférieure à la normale.
.. _class_Thread_constant_PRIORITY_NORMAL:
.. rst-class:: classref-enumeration-constant
:ref:`Priority<enum_Thread_Priority>` **PRIORITY_NORMAL** = ``1``
Un fil d'exécution avec une priorité normale.
.. _class_Thread_constant_PRIORITY_HIGH:
.. rst-class:: classref-enumeration-constant
:ref:`Priority<enum_Thread_Priority>` **PRIORITY_HIGH** = ``2``
Un fil d'exécution avec une priorité supérieure à la normale.
.. rst-class:: classref-section-separator
----
.. rst-class:: classref-descriptions-group
Descriptions des méthodes
--------------------------------------------------
.. _class_Thread_method_get_id:
.. rst-class:: classref-method
:ref:`String<class_String>` **get_id**\ (\ ) |const| :ref:`🔗<class_Thread_method_get_id>`
Returns the current **Thread**'s ID, uniquely identifying it among all threads. If the **Thread** has not started running or if :ref:`wait_to_finish()<class_Thread_method_wait_to_finish>` has been called, this returns an empty string.
.. rst-class:: classref-item-separator
----
.. _class_Thread_method_is_alive:
.. rst-class:: classref-method
:ref:`bool<class_bool>` **is_alive**\ (\ ) |const| :ref:`🔗<class_Thread_method_is_alive>`
Renvoie ``true`` si ce **Thread** exécute actuellement la fonction fournie. Ceci est utile pour déterminer si :ref:`wait_to_finish()<class_Thread_method_wait_to_finish>` peut être appelée sans bloquer le thread d'appel.
Pour vérifier si un **Thread** est joignable, utilisez :ref:`is_started()<class_Thread_method_is_started>`.
.. rst-class:: classref-item-separator
----
.. _class_Thread_method_is_started:
.. rst-class:: classref-method
:ref:`bool<class_bool>` **is_started**\ (\ ) |const| :ref:`🔗<class_Thread_method_is_started>`
Renvoie ``true`` si ce **Thread** a été lancé. Une fois lancé, cela renverra ``true`` jusqu'à ce qu'il soit joint en utilisant :ref:`wait_to_finish()<class_Thread_method_wait_to_finish>`. Pour vérifier si un **Thread** exécute encore sa tâche, utilisez :ref:`is_alive()<class_Thread_method_is_alive>`.
.. rst-class:: classref-item-separator
----
.. _class_Thread_method_set_thread_safety_checks_enabled:
.. rst-class:: classref-method
|void| **set_thread_safety_checks_enabled**\ (\ enabled\: :ref:`bool<class_bool>`\ ) |static| :ref:`🔗<class_Thread_method_set_thread_safety_checks_enabled>`
Sets whether the thread safety checks the engine normally performs in methods of certain classes (e.g., :ref:`Node<class_Node>`) should happen **on the current thread**.
The default, for every thread, is that they are enabled (as if called with ``enabled`` being ``true``).
Those checks are conservative. That means that they will only succeed in considering a call thread-safe (and therefore allow it to happen) if the engine can guarantee such safety.
Because of that, there may be cases where the user may want to disable them (``enabled`` being ``false``) to make certain operations allowed again. By doing so, it becomes the user's responsibility to ensure thread safety (e.g., by using :ref:`Mutex<class_Mutex>`) for those objects that are otherwise protected by the engine.
\ **Note:** This is an advanced usage of the engine. You are advised to use it only if you know what you are doing and there is no safer way.
\ **Note:** This is useful for scripts running on either arbitrary **Thread** objects or tasks submitted to the :ref:`WorkerThreadPool<class_WorkerThreadPool>`. It doesn't apply to code running during :ref:`Node<class_Node>` group processing, where the checks will be always performed.
\ **Note:** Even in the case of having disabled the checks in a :ref:`WorkerThreadPool<class_WorkerThreadPool>` task, there's no need to re-enable them at the end. The engine will do so.
.. rst-class:: classref-item-separator
----
.. _class_Thread_method_start:
.. rst-class:: classref-method
:ref:`Error<enum_@GlobalScope_Error>` **start**\ (\ callable\: :ref:`Callable<class_Callable>`, priority\: :ref:`Priority<enum_Thread_Priority>` = 1\ ) :ref:`🔗<class_Thread_method_start>`
Démarre un nouveau **Thread** qui appelle ``callable``.
Si la méthode prend des arguments, vous pouvez les passer en utilisant :ref:`Callable.bind()<class_Callable_method_bind>`.
La priorité ``priority`` du **Thread** peut être modifiée en passant une valeur de lénumération :ref:`Priority<enum_Thread_Priority>`.
Renvoie :ref:`@GlobalScope.OK<class_@GlobalScope_constant_OK>` lors du succès, ou :ref:`@GlobalScope.ERR_CANT_CREATE<class_@GlobalScope_constant_ERR_CANT_CREATE>` lors de l'échec.
.. rst-class:: classref-item-separator
----
.. _class_Thread_method_wait_to_finish:
.. rst-class:: classref-method
:ref:`Variant<class_Variant>` **wait_to_finish**\ (\ ) :ref:`🔗<class_Thread_method_wait_to_finish>`
Joins the **Thread** and waits for it to finish. Returns the output of the :ref:`Callable<class_Callable>` passed to :ref:`start()<class_Thread_method_start>`.
Should either be used when you want to retrieve the value returned from the method called by the **Thread** or before freeing the instance that contains the **Thread**.
To determine if this can be called without blocking the calling thread, check if :ref:`is_alive()<class_Thread_method_is_alive>` is ``false``.
.. |virtual| replace:: :abbr:`virtual (Cette méthode doit typiquement être redéfinie par l'utilisateur pour avoir un effet.)`
.. |required| replace:: :abbr:`required (This method is required to be overridden when extending its base class.)`
.. |const| replace:: :abbr:`const (Cette méthode n'a pas d'effets de bord. Elle ne modifie aucune des variables membres de l'instance.)`
.. |vararg| replace:: :abbr:`vararg (Cette méthode accepte n'importe quel nombre d'arguments après ceux décris ici.)`
.. |constructor| replace:: :abbr:`constructor (Cette méthode est utilisée pour construire un type.)`
.. |static| replace:: :abbr:`static (Cette méthode n'a pas besoin d'instance pour être appelée, elle peut donc être directement appelée en utilisant le nom de la classe.)`
.. |operator| replace:: :abbr:`operator (Cette méthode décrit un opérateur valide à utiliser avec ce type en tant qu'opérande gauche.)`
.. |bitfield| replace:: :abbr:`BitField (Cette valeur est un nombre entier composé d'un masque de bits des options suivantes.)`
.. |void| replace:: :abbr:`void (Aucune valeur de retour.)`