+1 vote

I can create a new Thread object and get a unique id string for it, but how can I get the currently running thread or its unique id string? Or equally useful for my purposes, how can I test if the currently running thread == a particular Thread that I've created?

I'm asking because I believe I can implement a particular function in a thread-safe way without locks, but only if I can evaluate what thread is calling the function.

There is an OS.set_thread_name(), but no get_thread_name(). I guess this is the string that I would get if I used Thread.get_id(), but haven't tested this. In any case, I don't want to change any thread names, just look at them from running code.

in Engine by (112 points)

2 Answers

+2 votes

Maybe you can save the thread id in the function itself.

func _button2_pressed(thread):
    var thid=thread.get_id()
    print("idx:",thid)

func _on_Button_pressed():
    var thread=Thread.new()
    var id=thread.start (self, "_button2_pressed", thread)

    var thread1=Thread.new()
    var id1=thread1.start (self, "_button2_pressed", thread1)
by (805 points)

Thanks! This will work if there really isn't a more direct way.

+2 votes
OS.get_thread_caller_id()

wierd but it works all the same

FYI:
I'm answering so random people who stumble on this post (you) get an answer because this post is 4 years old and i doubt the guy didn't find his answer

by (18 points)

I had a workaround but this answer is still useful for me. Thank you! (I guess this method appeared sometime since Godot 2.x.)

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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.