Hi all, if i want to run a function in a separated thread, how can i pass some parameter to it?
Like:
thread("myFunction(parameter)"); (i know that's not the way)
Thanks
Using a global variable like that would not allow the program to use more than one thread.
That's true only for a specific function. Of course we can use thread("") for another function! O:-)
But the undocumented thread("") is aimed for a quick & simple informal way to have some function running async along w/ Processing's draw().
For example, a function that fetches some HTML page from time to time w/o slowing down the "Animation Thread".
Of course, for more complex stuff, we're better off defining a class which either extendsThread or implementsRunnable: (*)
My original answer assumes that the user wants to pass some information to another thread. Maybe multiple threads. Maybe a hundred threads! An example might be loading 100 image files.
Using the above approach of setting the value of a global variable before creating the thread, then accessing that variable from the thread, will NOT work if you're dealing with more than one thread.
In the example of loading a hundred image files, say I use a global variable that contains the image file to be loaded. Okay, I set that variable, then call my loading function on another thread. Now I set that global variable to the next image to be loaded, and call it again.
The problem is pretty basic threading- that first thread might not be done loading its image when I set that global variable to the second image!
And even if the OP is only using one extra thread, imho the "global variable" approach is a pretty bad habit to get into. What happens when OP wants to expand the program to include two extra threads?
This example uses a message queue (which would conform to a "global variable", in other parallel programming environments one would call it shared memory) and notify() / wait() as messaging and semaphore mechanisms.
You may find other interesting articles using search terms "inter thread communication".
My original answer assumes that the user wants to pass some information to another thread. Maybe multiple threads. Maybe a hundred threads! An example might be loading 100 image files.
Sorry I expressed myself erroneously. What I meant was in most cases, thread("") is exclusively to invoke a separate function once, not multiple calls to it at the same time! As it is my HTML fetch idea which runs parallel to draw()!
In short, thread("") is just a quick way to have a function running in parallel w/o resorting to some formal Thread or Runnable class!
Besides, creating 1 Thread for each loadImage() is over the top! Just have 1 Thread to load them all while "Animation Thread" is displaying a progress bar! *-:)
And even if the OP is only using one extra thread, imho the "global variable" approach is a pretty bad habit to get into.
We can't change a callback signature. Be it for setup(), draw(), keyPressed(), etc.!
And both thread("") & method("") doesn't allow passed parameters either!
That's why we use "global" variables in Processing when we need callbacks to share data among themselves!
We can't change a callback signature. Be it for setup(), draw(), keyPressed(), etc.!
And both thread("") & method("") doesn't allow passed parameters either!
That's why we use "global" variables in Processing when we need callbacks to share data among themselves!
I understand that. And my argument is that if you need to pass information like that, it's a much better practice to use a Java thread like my first post.
Answers
Can you post an MCVE that we can play with?
Assuming you're talking about Java mode, you might want to just use a Java thread. Something like this:
And then to use that Thread, you just do this:
new MyThread("testing").start();
Note: I haven't actually tested any of this code, but this should give you the general idea.
Hmm. One possible way would be via global variable... I don't know if there's a proper way...
Using a global variable like that would not allow the program to use more than one thread.
That's true only for a specific function. Of course we can use thread("") for another function! O:-)
But the undocumented thread("") is aimed for a quick & simple informal way to have some function running async along w/ Processing's draw().
For example, a function that fetches some HTML page from time to time w/o slowing down the "Animation Thread".
Of course, for more complex stuff, we're better off defining a class which either
extends
Thread orimplements
Runnable: (*)I'm not sure what you mean by that.
My original answer assumes that the user wants to pass some information to another thread. Maybe multiple threads. Maybe a hundred threads! An example might be loading 100 image files.
Using the above approach of setting the value of a global variable before creating the thread, then accessing that variable from the thread, will NOT work if you're dealing with more than one thread.
In the example of loading a hundred image files, say I use a global variable that contains the image file to be loaded. Okay, I set that variable, then call my loading function on another thread. Now I set that global variable to the next image to be loaded, and call it again.
The problem is pretty basic threading- that first thread might not be done loading its image when I set that global variable to the second image!
And even if the OP is only using one extra thread, imho the "global variable" approach is a pretty bad habit to get into. What happens when OP wants to expand the program to include two extra threads?
While I have not tried this, see
http://javarevisited.blogspot.ch/2013/12/inter-thread-communication-in-java-wait-notify-example.html
This example uses a message queue (which would conform to a "global variable", in other parallel programming environments one would call it shared memory) and notify() / wait() as messaging and semaphore mechanisms.
You may find other interesting articles using search terms "inter thread communication".
Sorry I expressed myself erroneously. What I meant was in most cases, thread("") is exclusively to invoke a separate function once, not multiple calls to it at the same time! As it is my HTML fetch idea which runs parallel to draw()!
In short, thread("") is just a quick way to have a function running in parallel w/o resorting to some formal Thread or Runnable class!
Besides, creating 1 Thread for each loadImage() is over the top! Just have 1 Thread to load them all while "Animation Thread" is displaying a progress bar! *-:)
We can't change a callback signature. Be it for setup(), draw(), keyPressed(), etc.!
And both thread("") & method("") doesn't allow passed parameters either!
That's why we use "global" variables in Processing when we need callbacks to share data among themselves!
I understand that. And my argument is that if you need to pass information like that, it's a much better practice to use a Java thread like my first post.