game/godot

Godot - Thread safe APIs

C/H 2018. 12. 13. 08:30

Thread safe APIs


Threads

Using threads is a common way to balance processing scatter it across CPUs and cores. Godot supports multi threading, but not in the whole engine.
쓰레드를 사용하는 것은 CPU와 코어에서 프로세싱을 분산시키는 일반적인 방법입니다. Godot은 멀티 스레딩을 지원하지만 전체 엔진에서는 지원하지 않습니다.

Below is a list of the areas in Godot and how they can be used with threads.
아래는 Godot의 영역 목록과 스레드와 함께 사용할 수있는 방법입니다.

Global scope

Global Scope singletons are all thread safe. Accessing servers from threads is supported (for VisualServer and Physics servers, ensure threaded or thread safe operation is enabled in the project settings!).
전역 범위 싱글 톤은 모두 스레드로부터 안전합니다. 스레드에서 서버에 액세스 할 수 있습니다 (VisualServer 및 Physics 서버의 경우 스레드 설정 또는 스레드 안전 작업이 프로젝트 설정에서 활성화되어 있는지 확인하십시오).

This makes them ideal for code that creates dozens of thousands of instances in servers and controls them from threads. Of course, it requires a bit more code, as this is used directly and not within the scene tree.
따라서 수십만 개의 인스턴스를 서버에 생성하고 스레드에서 제어하는 코드에 이상적입니다. 물론 이것은 장면 트리가 아닌 직접 사용되기 때문에 조금 더 많은 코드가 필요합니다.

Scene tree

Interacting with the active scene tree is NOT thread safe. Make sure to use mutexes when sending data between threads. If you want to call functions from a thread, the call_deferred function may be used:
활성 장면 트리와 상호 작용하는 것은 스레드로부터 안전하지 않습니다. 스레드간에 데이터를 보낼 때는 반드시 뮤텍스를 사용해야합니다. 스레드에서 함수를 호출하려면 call_deferred 함수를 사용할 수 있습니다.

# Unsafe:
node.add_child(child_node)
# Safe:
node.call_deferred("add_child", child_node)

However, creating scene chunks (nodes in tree arrangement) outside the active tree is fine. This way, parts of a scene can be built or instantiated in a thread, then added in the main thread:
그러나 활성 트리 외부에 장면 청크 (트리 배열의 노드)를 만드는 것이 좋습니다. 이렇게하면 장면의 일부분을 스레드에서 빌드하거나 인스턴스화 한 다음 주 스레드에 추가 할 수 있습니다.

var enemy_scene = load("res://enemy_scene.scn").instance()
var enemy = enemy_scene.instance()
enemy.add_child(weapon) # Set a weapon.
world.call_deferred("add_child", enemy)

GDScript arrays, dictionaries

In GDScript, reading and writing elements from multiple threads is ok, but anything that changes the container size (resizing, adding or removing elements) requires locking a mutex.
GDScript에서는 여러 스레드에서 요소를 읽고 쓰는 것이 좋습니다. 그러나 컨테이너 크기를 변경하는 모든 요소 (크기 조정, 요소 추가 또는 제거)에는 뮤텍스 잠금이 필요합니다.

Resources

Modifying a unique resource from multiple threads is not supported, but loading them on threads or handling a reference is perfectly supported. Scenes, textures, meshes, etc. Can be loaded and manipulated on threads, then added to the active scene in the main thread.
여러 스레드에서 고유 자원을 수정하는 것은 지원되지 않지만 스레드에로드하거나 참조를 처리하는 것은 완벽하게 지원됩니다. 장면, 텍스처, 메시 등 스레드에서로드 및 조작 한 다음 메인 스레드의 활성 장면에 추가 할 수 있습니다.

반응형

'game > godot' 카테고리의 다른 글

Godot - Google Play Service Lederboard Setting Problem  (0) 2018.12.24
GodotFireBase Module Compile Error  (0) 2018.12.17
Godot - Compiling  (0) 2018.12.12
Godot - Getting the source  (0) 2018.12.12
Godot - Introduction to the buildsystem  (0) 2018.12.11