How to multithread my allRGB code

edited September 2015 in Questions about Code

Hello, I'm new here. My english level is not very high. sorry for that!

I'm not a computer engineer (yet), it's a hobby, because I'm learning.

My PC:

-Core 2 Quad 2,4GHz (4 cores)

-4GB RAM

-Windows 7 64 bit

-Processing 2.2.1

In the last three years I have worked in an allRGB algorithm, and I want to improve that, just for learn:

http://allrgb.com/abner

http://elrincondeabner.blogspot.com.es/2015/05/allrgb.html

http://elrincondeabner.blogspot.com.es/2015/08/allrgb-mediante-algoritmo-genetico.html

I recently managed to apply dithering to my code, and works great!

But until today I have not managed to multithread my code. The code is VERY VERY long, so for simplicity I prefer to work whith a very simpler one. It is the problem:

How to multithread this code and use all of the 4 cores my PC have?

println("Start");
int a=0,b=0,c=0,d=0;
loadPixels();
for (int i=0; i<100000; i++){
  a=a+1;  //Thread 1
  b=b+2;  //Thread 2
  c=c+3;  //Thread 3
  d=d+4;  //Thread 4
}
updatePixels();
exit();
println("END");

I need the code, please.

Answers

  • Do you understand the question?

    What I want to do is simply make a faster algorithm by using my 4 cores. I searched info but I don't understand/managed how to implement it.

  • Keep in mind that Processing.js does not support threads at all, so by doing this, you're restricting yourself to only deploying as a Java application.

    But the general idea is that you need to isolate each part of your code that can run without affecting other parts of your code. Those isolated parts of your code are things that you might use multithreading for.

    Threading is not trivial, and there are any number of ways to use it- some are right, many are wrong. But in keeping with your example, one way to use threads might be something like this:

    void setup() {
      println("Start");
    
    //create 4 threads
      MyThread t1 = new MyThread(1);
      MyThread t2 = new MyThread(2);
      MyThread t3 = new MyThread(3);
      MyThread t4 = new MyThread(4);
    
    //start the threads
      t1.start();
      t2.start();
      t3.start();
      t4.start();
    
      //wait for each thread to finish
      try {
        t1.join();
        t2.join();
        t3.join();
        t4.join();
      }
      catch(InterruptedException e) {
        e.printStackTrace();
      }
    
    //get the value from each thread
      println("T1: " + t1.x);
      println("T2: " + t2.x);
      println("T3: " + t3.x);
      println("T4: " + t4.x);
    
    //we're done
      println("END");
      exit();
    }
    
    class MyThread extends Thread {
    
      int x = 0;
      int add;
    
      public MyThread(int add) {
        this.add = add;
      }
    
      //the run function of one thread runs "at the same time"
      //as the run function in other threads 
      public void run() {
        println("Thread " + add + " is starting.");
        for (int i=0; i<100000; i++) {
          x += add;
        }
        println("Thread " + add + " is done.");
      }
    }
    

    It's important to note that you have to be absolutely sure that the threads aren't accessing the same data at the same time, otherwise really weird things can happen.

    There's a lot more to threading than just this simple example, but these are the basics.

  • Oh man, Thanks very much!

    I will try it :DDDD

Sign In or Register to comment.