0 votes

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):
    mutex.lock()
    _config_file.save(SAVE_PATH)
    mutex.unlock()

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?

asked Mar 19 in Engine by bashan (47 points)

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?

  • 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, "configsave") several times from different places of my code according to events.

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.

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?

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..

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.