How to pass parameter to function in separated thread

edited March 2018 in How To...

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

Tagged:

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:

    class MyThread extends Thread{
    
       String parameter;
    
       public MyThread(String parameter){
          this.parameter = parameter;
       }
    
       public void run(){
          System.out.println(parameter);
       }
    }
    

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

    int perc = 0;
    
    void setup() {
      size(300, 300);
      noStroke();
      thread("updater");
    }
    
    void draw() {
      background(0);
      fill(255, 0, 0);
      rect(10, 10, width-20, 20);
      fill(0, 255, 0);
      rect(10, 10, map(perc, 0, 100, 0, width-20), 20);
    }
    
    void updater() {
      while (perc < 100) {
        if (random(1)<.2) perc++;
        delay(100);
        if (PARAMETER_RESTART) {
          perc = 0;
          PARAMETER_RESTART = false;
        }
        if (PARAMETER_SETTO) {
          perc = new_value;
          PARAMETER_SETTO = false;
        }
      }
    }
    
    boolean PARAMETER_RESTART = false;
    boolean PARAMETER_SETTO = false;
    int new_value = 0;
    
    void mousePressed() {
      new_value = 50;
      PARAMETER_SETTO = true;
    }
    
    void keyPressed() {
      PARAMETER_RESTART = true;
    }
    
  • Using a global variable like that would not allow the program to use more than one thread.

  • 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 or implements Runnable: (*)

  • That's true only for a specific function.

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

  • edited December 2014

    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.

Sign In or Register to comment.