Godot "Thread is already running" issue

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By bashan

I am using a thread to save user settings in the background.
It uses a Thread and a Mutex.

This is how I save the settings:

thread.start(self, "_config_save")

This is the thread function:

func _config_save(userdata):

The function works for the first time and then I get this error:

ERROR: Thread already started.
   At: core/bind/core_bind.cpp:2700

Anything I am doing wrong?

Looks about right. You don’t get any stack trace along with the error? You are sure, you’re not calling thread.start multiple times somehow? Do you have this problem in other projects / with other callbacks as well? Can you provide an example project?

njamster | 2020-03-20 13:53

  • No stack trace. Just this message.
  • I am calling Thread.start multiple times. Isn’t that the whole point of threads? I save my game several times (according to different events) and want it to happen in the background so main thread will not be laggy.
  • Didn’t try it with other projects. First time I use it…
  • I supplied the main part of the code. Please note as mentioned above, I do call: thread.start(self, “_config_save”) several times from different places of my code according to events.

bashan | 2020-03-20 21:14

I am calling Thread.start multiple times. Isn’t that the whole point of threads?

You can start multiple threads. You cannot start the same thread multiple times.

So the following won’t work:

var thread = Thread.new()
thread.start(self, "_config_save")
thread.start(self, "_config_save")

But something like this will:

thread1 = Thread.new()
thread1.start(self, "_config_save")
thread2 = Thread.new()
thread2.start(self, "_config_save")

However, if I run the former example I get a stack trace along with the error, pointing me precisely to the line of code producing the error. No idea why you wouldn’t…

I supplied the main part of the code.

And again: It looks about right! The error is (very likely) somewhere else in your code. That’s why I asked for an example project. Without a more complete example, I doubt anyone will be able to help you here.

njamster | 2020-03-21 09:44

If I can do “start” only once, then the error is pretty obvious: I am doing “start” more than once on the same instance. I just don’t do it at once. I call a method multiple times in different times of the sames. So I wouldn’t wonder why it works on the first time and then fails on all the other times. No the question: how do I handle my use-case properly: I want to be able to save my game on the background (using a thread). The game can be saved from multiple “places” in the code according to events. So I do have to create a thread for each “save”. But I only have a single thread instance. Should I manage some thread queue in this case?

bashan | 2020-03-21 11:21

I solved this issue using a semaphore and a single thread. The thread runs indefinitely and I use the semaphore to activate it whenever I need. Hope it is a reasonable solution…

bashan | 2020-03-21 11:56