-
-
Notifications
You must be signed in to change notification settings - Fork 21.2k
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
Godot stutters when starting a thread. #24869
Comments
It says the zip file cannot be found. It might be the |
I might be doing something wrong, but I'm having the same issue. Below is my take on the code. This should be running butter smooth since it should be using the same thread as opposed to spinning up new ones. I'm more of a C# guy, so I might be doing something wrong. I'm going to try and see if calling
|
I just followed this demo project from the Godot team. How I understand it:
What do you think? Edit: Updated the demo project |
@Ploppy3 I think you're doing everything right and I think you found a bug. So in this test, it shows something is definitely going on. The I wish I had more time to dig into this, but my play time is now over and I need to get back to my real job :)
|
The only solution that you might be able to do is manage when you're done with your thread yourself (in an array or something), and just never call Pseudo:
|
Can't confirm on linux using the sample project, potentially a windows-only problem. |
I can confirm with @bojidar-bg that it doesn't affect Linux. I'm on Pop OS 18.04. Edit: The same code I posted runs butter smooth. |
I can confirm the problem on windows. Windows 10, amd fx-8350, gtx770 |
I just checked on my Mac which is a 2008 Mac Pro (Cheesegrater) running High Sierra. Everything looks good there too. So, it must be a Windows bug. I was running Windows 10 also. |
Recently did a bunch of experiments with threading and can confirm as well on Windows 10 I have been having spikes and frame loss issues with running threads. Was thinking it was because of the interactive loaders I was using, but found that just kicking a bunch of threads can cause severe drops in frame rate for very minor tasks. I was hoping to take advantage of doing background work ahead of time to reduce load times, but at present it may not be viable. |
@akien-mga thank you for setting the milestone to 3.1! Much appreciated! |
It seems to be |
By the way you MUST use wait_to_finish after exiting a thread, else the thread is never freed. This is a Windows thing. I wonder if there is a better way to do it nowadays though. And yes, the stutter due to this is normal on Windows, not sure if much else can be done. |
In general, the best way to use threads is to start them and leave them running and give data to them via semaphore. |
I'm quite confused, do you mean that Godot will always freeze when starting a thread? Also, how can I reuse a thread when I need to call:
Edit: I originally thought I was reusing my thread by calling |
New to this, and not sure either, as it at least seems that the Thread object just handles threads in some invisible way, and is not a thread itself. Wouldn't the start and wait still be an issue? Is there an example out there of how to safely reuse a Godot thread over the duration of a game? |
@reduz This is how godot/drivers/windows/thread_windows.cpp Lines 80 to 87 in 93d8f3c
I don't think joining the thread is required to avoid leaking. AFAIK it would be enough to just close the handle. Have a look at this BSD-licensed implementation of |
Fixes godotengine#24869 stuttering on Windows by reusing long running threadpool threads instead of creating a new thread on each call to Thread::start.
So just to make sure, this PR is not fixing the thread creation stutter, but instead let Godot reuse existing threads. I don't think the issue should be closed, what do you think? |
Is there a way to make it better on windows? If there is a way which works as good as on unix, we might use that instead, otherwise we can just mention it as a platform limitation in the docs. |
Is it a generalized problem? I never heard of this before. |
#25213 is an alternative fix that uses a dedicated thread pool (so a minimum number of pre-allocated worker threads can be set) |
@dragmz I can confirm your first fix complete solve the problem since I'm now reusing the thread. Went from unplayable to constant 60 fps when running many nav_agent on a big aStar grid. |
@Ploppy3 Good to hear that! I've noticed a slightly lower FPS when the first Godot thread call is made which I believe is because a new thread is added to the default thread pool. The latter fix uses pre-allocated threads so it should eliminate the issue. |
I really like the performance boost so I don't mind the second fix being pushed to 3.2! Really quick and great job! Thank you! |
Fixes godotengine#24869 stuttering on Windows by reusing long running threadpool threads instead of creating a new thread on each call to Thread::start.
Has there been a regression on this issue? There's a very noticeable stutter upon calling Here's some code to test it: func thread_func():
print("test")
var thread = Thread.new()
func _process(delta):
thread.wait_to_finish()
thread.start(self,"thread_func") this issue is really getting in the way of what I'm trying to do here update: |
Godot version:
3.1-beta1
also tested on 3.0.6
OS/device including version:
Windows 10
GTX 1060
i5 2500k
Issue description:
Every time I start a thread, the game stutters, which defeats the purpose of using a thread.
Considering I'm new to threading the problem may be caused by my implementation.
Steps to reproduce:
Minimal reproduction project:
test-threading.zip
The text was updated successfully, but these errors were encountered: