-
-
Notifications
You must be signed in to change notification settings - Fork 21.1k
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
Thread does not start with a method with no arguments #9924
Comments
Well, I think it's the expected behavior. You should be getting an error like this in the console so you get a clue on what's going on:
|
I think Threads should still start even if there are no parameters defined in the Thread method signature.
My proposal:
|
I think it should match the default function call behavior: if the number of arguments in the call doesn't match the function arity, the game should break and the debugger open with an error. IMO setting the parameters to |
@vnen Yes, but even if you call Is it possible to pass the arguments as real parameters (not as an array) to the thread method? |
@timoschwarzer, I'm confused. Isn't the problem that if you pass no arguments then the thread gets no arguments as well and that causes an error? Or do you mean passing an empty array? |
No, if you pass no arguments the parameter in the start method defaults to an empty array. The OP's problem is, that the thread doesn't start even if there are no arguments given. This is intended. Now we can discuss if the thread should start if the thread method has no parameters defined ( |
Why? The If you method is
Well, AFAIU, it already works, no? If there's no parameters required, you pass no arguments aka an empty array. |
No, AFAIK the parameters are passed as an array to the method. |
No, it's an array of parameters, not the parameter itself. Empty array means no parameters. If it behaves differently, it's a bug. |
This:
prints:
|
Ok, then that's a bug IMO. It should be:
prints:
i.e. just like |
I think this is modeled after the usual threading APIs limitation of being able to pass only one user data varaible. Of course, Godot could abstract that out, but it makes sense to me. Anyway if thas was going to be changed, it should be done for 3.0. |
Then the doc is wrong, too:
|
Ah well if that's how threads are usually designed, we should likely keep it like that. |
If we keep it like that we should consider allowing 0 parameters for convenience reasons and to prevent confusion. |
POSIX threads:
Windows threads:
C++11 threads seem to allow for multiple arguments, but I've not checked them personally. |
Anyway. it doesn't have to be an array. You can pass a plain variable if you don't need multiple values. |
But about the error reporting. What about?:
|
Sounds good, though:
I guess |
That's right. There would be an ambiguity about explicit vs. default null, but not much can be done about it. |
So I guess in the end we should just enforce one argument in all cases and just make the debugger helpful enough? |
I think so, because the error shouldn't depend on passing null or non-null. I mean, if the worker fn had no arguments and the calling code were wrongly passing some value in, sometimes null but non-null on edge cases, you may overlook the problem. In other words, you would be loosing a chance of dynamic analysis of the code. At least, with the parameter enforcement, you can know you are doing something wrong as soon as start() is called. |
I was about to implement this, but something is not clear to me. In order to let the debugger break, I think I should call Should this behavior of letting the debugger kick in be only available when using What do you think? |
Its extremely confusing to have the Thread.start() fail if the method called in the thread has no arguments. If I don't pass any userdata to the Thread.start() method then it should not try passing an empty array to the method I'm calling in the thread. |
Still valid in the 3.x branch as per #18235. |
I think that it's a good idea to allow multiple and no parameters, RN you must run in on a "compatibility" function that executes the final wanted function with your correct (or no) arguments, so the best would be to pass arguments as an array: func _ready():
var thread = Thread.new()
thread.create(self, "method", ["Hello World ", 2, 4.20])
func method(text, x, y):
print(String, x*y) Output:Hello World 8.4 |
An option would be to make |
I like the idea of having a reserved word to execute a thread automatically in the language itself. func _ready():
var thread = method("blabla", 2, 3)
# Thread.new().create(self, "method", ["blabla", 2, 3])
async method(text, x, y):
print(text)
print(x * x + y) |
@schweigert func _ready():
var thread = async my_method(1, 2, 3)
var no_thread = my_method(1, 2, 3)
func my_method(a, b, c):
# do work
pass This would still allow us to call the method synchronously. I don't know if it's worth a new syntax sugar keyword though. |
@timoschwarzer I think so. New languages like Kotlin and Golang are using parallel programming in this way. |
@schweigert it's better if you open a new issue with your suggestion. |
Was making a thread in my demo project today and i made a thread with no argument and ofc it did not work, and then after awhile i remembered this error :) |
Did someone fix this bug? It seems the workaround in the Voxel demo isn't necessary in 3.4. godotengine/godot-demo-projects#639 |
It do not work today @aaronfranke |
@schweigert What version did you try? It is very likely fixed in the latest |
I can confirm that it's fixed in 3.4 beta 2 (so it was fixed by #38078). |
ElementaryOS 0.4.1 (Ubuntu 17) - Godot version: 2.1.3
Thread does not start with a method with no arguments
This node works:
But this does not:
I found this error during my question (https://godotengine.org/qa/16730/thread-not-running), but only by adding the argument did the method start to run.
The text was updated successfully, but these errors were encountered: