Giter VIP home page Giter VIP logo

godot-thread-pool's Introduction

Godot Thread Pool

Thread pool implementation in GDScript for running multiple tasks in parallel. Unlike other implementations I have encountered this doesn't leak memory when the pool is unused and threads are joined eventually after the ThreadPool instance is freed from memory.

Usage

Constructing a ThreadPool instance takes two optional arguments:

  • min_threads for the minimum number of threads to start with. [optional]
  • max_threads for a maximum of threads to allocate when needed. [optional]

Sending a task to the thread pool is fairly easy via the submit_task method, which takes 2 arguments:

  • function a FuncRef.
  • arguments an array of arguments to pass to function. [optional]

The task will then be pushed into a queue until a thread claims it and executes it.

sumbit_task is a coroutine, you can use yield() to wait for task completion or retrieving the return value.

Code example:

# Instantiate a thread pool
# with a minimum of 5 threads and maximum of 10
var thread_pool: = ThreadPool.new(5, 10)

func example() -> void:
	# queue a task to run inside a thread
	thread_pool.submit_task(funcref(self, "some_task1"))

	# wait for the task to finish
	var state = thread_pool.submit_task(funcref(self, "some_task2"), [2, 5])
	var result: int = yield(state, "completed")

	assert(result == 7)

func some_task1() -> void:
	# some processing ...
	pass

func some_task2(a: int, b: int) -> int:
	return a + b

godot-thread-pool's People

Contributors

abdera7mane avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.