Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update WorkerThreadPool doc to mention every task should be waited so that any allocated resources can be cleaned up. #84926

Merged
merged 1 commit into from
Apr 23, 2024

Conversation

jsjtxietian
Copy link
Contributor

Fixes #84888

@jsjtxietian jsjtxietian requested a review from a team as a code owner November 15, 2023 04:15
@j-zeppenfeld
Copy link

This is certainly the easy way forward, but I wouldn't exactly call it a fix.

The documentation for WorkerThreadPool explicitly states:

This can be used for simple multithreading without having to create Threads.

Having to track task ids and make sure to call wait_for_task_completion without blocking the main thread is not "simple".

@MewPurPur
Copy link
Contributor

I wrote the docs without much knowledge on the subject matter, only some testing. I wouldn't take them for gospel, moreover, this seems like a technical limitation. And it's still simpler than multithreading with Thread.

@kleonc
Copy link
Member

kleonc commented Nov 15, 2023

Seems this should also resolve #84899 (it's kinda a duplicate of #84888).

While at it, it's probably a good idea to also add some notes to is_task_completed/is_group_task_completed that they return a meaningful value only between adding the task and awaiting its completion / that they error in case of checking completion status of a task already awaited completion.

var id = WorkerThreadPool.add_task(some_task)
...
WorkerThreadPool.is_task_completed(id) # false/true
...
WorkerThreadPool.wait_for_task_completion(id)
WorkerThreadPool.is_task_completed(id) # false, and "Invalid Task ID" error

bool WorkerThreadPool::is_task_completed(TaskID p_task_id) const {
task_mutex.lock();
const Task *const *taskp = tasks.getptr(p_task_id);
if (!taskp) {
task_mutex.unlock();
ERR_FAIL_V_MSG(false, "Invalid Task ID"); // Invalid task
}
bool completed = (*taskp)->completed;
task_mutex.unlock();
return completed;
}

bool WorkerThreadPool::is_group_task_completed(GroupID p_group) const {
task_mutex.lock();
const Group *const *groupp = groups.getptr(p_group);
if (!groupp) {
task_mutex.unlock();
ERR_FAIL_V_MSG(false, "Invalid Group ID");
}
bool completed = (*groupp)->completed.is_set();
task_mutex.unlock();
return completed;
}

at some point so that any allocated resources can be cleaned up.
Copy link
Member

@RandomShaper RandomShaper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A fire-and-forget API could be added in the future, provided tracking task ids is really that painful. In the meantime, this PR adds sensitve clatifications.

@akien-mga akien-mga modified the milestones: 4.x, 4.3 Apr 23, 2024
@akien-mga akien-mga merged commit b39afcd into godotengine:master Apr 23, 2024
16 checks passed
@akien-mga
Copy link
Member

Thanks!

@jsjtxietian jsjtxietian deleted the update-worker-thread-doc branch April 24, 2024 02:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

WorkerThreadPool leaks memory and occasionally crashes
7 participants