mirror of
https://github.com/godotengine/godot-docs-l10n.git
synced 2026-01-04 10:09:56 +03:00
212 lines
12 KiB
ReStructuredText
212 lines
12 KiB
ReStructuredText
:github_url: hide
|
||
|
||
.. DO NOT EDIT THIS FILE!!!
|
||
.. Generated automatically from Godot engine sources.
|
||
.. Generator: https://github.com/godotengine/godot/tree/4.2/doc/tools/make_rst.py.
|
||
.. XML source: https://github.com/godotengine/godot/tree/4.2/doc/classes/WorkerThreadPool.xml.
|
||
|
||
.. _class_WorkerThreadPool:
|
||
|
||
WorkerThreadPool
|
||
================
|
||
|
||
**继承:** :ref:`Object<class_Object>`
|
||
|
||
单例,启动时会分配一些 :ref:`Thread<class_Thread>`\ ,可以将任务卸载到这些线程中执行。
|
||
|
||
.. rst-class:: classref-introduction-group
|
||
|
||
描述
|
||
----
|
||
|
||
**WorkerThreadPool** 单例在项目启动时会分配一组 :ref:`Thread<class_Thread>`\ (称作工作线程)并提供将任务卸载至这些线程上执行的方法。这样就能够简化多线程的使用,不必创建 :ref:`Thread<class_Thread>`\ 。
|
||
|
||
任务里放置的是要让线程执行的 :ref:`Callable<class_Callable>`\ 。\ **WorkerThreadPool** 既可以创建常规任务也可以创建分组任务,常规任务由单个工作线程执行,而分组任务可以分布在多个工作线程执行。分组任务会多次执行同一个 :ref:`Callable<class_Callable>`\ ,可用于遍历大量的元素,例如场景中的敌人。
|
||
|
||
以下是将开销很大的函数卸载到工作线程执行的例子:
|
||
|
||
|
||
.. tabs::
|
||
|
||
.. code-tab:: gdscript
|
||
|
||
var enemies = [] # 用敌人填充的数组。
|
||
|
||
func process_enemy_ai(enemy_index):
|
||
var processed_enemy = enemies[enemy_index]
|
||
# 开销很大的逻辑……
|
||
|
||
func _process(delta):
|
||
var task_id = WorkerThreadPool.add_group_task(process_enemy_ai, enemies.size())
|
||
# 其他代码……
|
||
WorkerThreadPool.wait_for_group_task_completion(task_id)
|
||
# 要求敌人 AI 已经处理完毕的其他代码。
|
||
|
||
.. code-tab:: csharp
|
||
|
||
private List<Node> _enemies = new List<Node>(); // 用敌人填充的数组。
|
||
|
||
private void ProcessEnemyAI(int enemyIndex)
|
||
{
|
||
Node processedEnemy = _enemies[enemyIndex];
|
||
// 开销很大的逻辑……
|
||
}
|
||
|
||
public override void _Process(double delta)
|
||
{
|
||
long taskId = WorkerThreadPool.AddGroupTask(Callable.From<int>(ProcessEnemyAI), _enemies.Count);
|
||
// 其他代码……
|
||
WorkerThreadPool.WaitForGroupTaskCompletion(taskId);
|
||
// 要求敌人 AI 已经处理完毕的其他代码。
|
||
}
|
||
|
||
|
||
|
||
以上代码要求 ``enemies`` 数组中的元素个数在多线程部分执行时保持不变。
|
||
|
||
\ **注意:**\ 如果分布到多个线程执行的任务在计算方面的开销并不大,那么使用这个单例可能对性能有负面影响。
|
||
|
||
.. rst-class:: classref-introduction-group
|
||
|
||
教程
|
||
----
|
||
|
||
- :doc:`使用多线程 <../tutorials/performance/using_multiple_threads>`
|
||
|
||
- :doc:`线程安全的 API <../tutorials/performance/thread_safe_apis>`
|
||
|
||
.. rst-class:: classref-reftable-group
|
||
|
||
方法
|
||
----
|
||
|
||
.. table::
|
||
:widths: auto
|
||
|
||
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`int<class_int>` | :ref:`add_group_task<class_WorkerThreadPool_method_add_group_task>` **(** :ref:`Callable<class_Callable>` action, :ref:`int<class_int>` elements, :ref:`int<class_int>` tasks_needed=-1, :ref:`bool<class_bool>` high_priority=false, :ref:`String<class_String>` description="" **)** |
|
||
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`int<class_int>` | :ref:`add_task<class_WorkerThreadPool_method_add_task>` **(** :ref:`Callable<class_Callable>` action, :ref:`bool<class_bool>` high_priority=false, :ref:`String<class_String>` description="" **)** |
|
||
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`int<class_int>` | :ref:`get_group_processed_element_count<class_WorkerThreadPool_method_get_group_processed_element_count>` **(** :ref:`int<class_int>` group_id **)** |const| |
|
||
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`bool<class_bool>` | :ref:`is_group_task_completed<class_WorkerThreadPool_method_is_group_task_completed>` **(** :ref:`int<class_int>` group_id **)** |const| |
|
||
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`bool<class_bool>` | :ref:`is_task_completed<class_WorkerThreadPool_method_is_task_completed>` **(** :ref:`int<class_int>` task_id **)** |const| |
|
||
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| void | :ref:`wait_for_group_task_completion<class_WorkerThreadPool_method_wait_for_group_task_completion>` **(** :ref:`int<class_int>` group_id **)** |
|
||
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`Error<enum_@GlobalScope_Error>` | :ref:`wait_for_task_completion<class_WorkerThreadPool_method_wait_for_task_completion>` **(** :ref:`int<class_int>` task_id **)** |
|
||
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
|
||
.. rst-class:: classref-section-separator
|
||
|
||
----
|
||
|
||
.. rst-class:: classref-descriptions-group
|
||
|
||
方法说明
|
||
--------
|
||
|
||
.. _class_WorkerThreadPool_method_add_group_task:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
:ref:`int<class_int>` **add_group_task** **(** :ref:`Callable<class_Callable>` action, :ref:`int<class_int>` elements, :ref:`int<class_int>` tasks_needed=-1, :ref:`bool<class_bool>` high_priority=false, :ref:`String<class_String>` description="" **)**
|
||
|
||
将 ``action`` 添加为分组任务,能够被多个工作线程执行。该 :ref:`Callable<class_Callable>` 的调用次数由 ``elements`` 决定,第一个调用的线程使用 ``0`` 作为参数,后续执行时会将其加 1,直到变为 ``element - 1``\ 。
|
||
|
||
任务分布的线程数由 ``tasks_needed`` 定义,默认值 ``-1`` 表示分布到所有工作线程。\ ``high_priority`` 决定的是任务具有高优先级还是低优先级(默认)。你还可以选择提供 ``description`` 作为描述信息,方便调试。
|
||
|
||
返回分组任务 ID,可用于其他方法。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_WorkerThreadPool_method_add_task:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
:ref:`int<class_int>` **add_task** **(** :ref:`Callable<class_Callable>` action, :ref:`bool<class_bool>` high_priority=false, :ref:`String<class_String>` description="" **)**
|
||
|
||
将 ``action`` 添加为分组任务,能够被单个工作线程执行。\ ``high_priority`` 决定的是任务具有高优先级还是低优先级(默认)。你还可以选择提供 ``description`` 作为描述信息,方便调试。
|
||
|
||
返回任务 ID,可用于其他方法。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_WorkerThreadPool_method_get_group_processed_element_count:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
:ref:`int<class_int>` **get_group_processed_element_count** **(** :ref:`int<class_int>` group_id **)** |const|
|
||
|
||
返回具有给定 ID 的分组任务的 :ref:`Callable<class_Callable>` 已经被工作线程执行的次数。
|
||
|
||
\ **注意:**\ 线程已经开始执行 :ref:`Callable<class_Callable>` 但尚未完成的情况不计算在内。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_WorkerThreadPool_method_is_group_task_completed:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
:ref:`bool<class_bool>` **is_group_task_completed** **(** :ref:`int<class_int>` group_id **)** |const|
|
||
|
||
如果具有给定 ID 的分组任务已经完成,则返回 ``true``\ 。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_WorkerThreadPool_method_is_task_completed:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
:ref:`bool<class_bool>` **is_task_completed** **(** :ref:`int<class_int>` task_id **)** |const|
|
||
|
||
如果具有给定 ID 的任务已经完成,则返回 ``true``\ 。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_WorkerThreadPool_method_wait_for_group_task_completion:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
void **wait_for_group_task_completion** **(** :ref:`int<class_int>` group_id **)**
|
||
|
||
在具有给定 ID 的分组任务完成前暂停调用这个方法的线程。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_WorkerThreadPool_method_wait_for_task_completion:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
:ref:`Error<enum_@GlobalScope_Error>` **wait_for_task_completion** **(** :ref:`int<class_int>` task_id **)**
|
||
|
||
暂停调用该方法的线程,直到给定 ID 对应的任务完成。
|
||
|
||
如果能够成功等待任务,则返回 :ref:`@GlobalScope.OK<class_@GlobalScope_constant_OK>`\ 。
|
||
|
||
如果不存在与传入 ID 对应的任务(可能已被等待或处理),则返回 :ref:`@GlobalScope.ERR_INVALID_PARAMETER<class_@GlobalScope_constant_ERR_INVALID_PARAMETER>`\ 。
|
||
|
||
如果其他正在执行的任务调用了该方法,并且由于任务调度的原因,要等待的任务位于调用栈更下层的位置,因此无法继续,则返回 :ref:`@GlobalScope.ERR_BUSY<class_@GlobalScope_constant_ERR_BUSY>`\ 。这是比较高级的情况,只有任务之间存在依赖关系时才会出现。
|
||
|
||
.. |virtual| replace:: :abbr:`virtual (本方法通常需要用户覆盖才能生效。)`
|
||
.. |const| replace:: :abbr:`const (本方法没有副作用。不会修改该实例的任何成员变量。)`
|
||
.. |vararg| replace:: :abbr:`vararg (本方法除了在此处描述的参数外,还能够继续接受任意数量的参数。)`
|
||
.. |constructor| replace:: :abbr:`constructor (本方法用于构造某个类型。)`
|
||
.. |static| replace:: :abbr:`static (调用本方法无需实例,所以可以直接使用类名调用。)`
|
||
.. |operator| replace:: :abbr:`operator (本方法描述的是使用本类型作为左操作数的有效操作符。)`
|
||
.. |bitfield| replace:: :abbr:`BitField (这个值是由下列标志构成的位掩码整数。)`
|