We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Multithreading? (Read 2766 times)
Multithreading?
Jun 20th, 2005, 5:31am
 
How would one go about creating multiple threads in processing?

I basically want to create 2 programs running side by side. I have a program that mines information from an active webcam, this is SLOW and runs at around 15fps. I also have a program that draws motion graphics based on the information the first program generates, on its own it can do 30 or 40 fps easy. So under 1 thread the whole mess runs at around 14 or 15 fps, but if I could impliment this as 2 threads then I could have the datamining going as slow as it needs to go without slowing down the animation, yes?

Any suggestions?
Re: Multithreading?
Reply #1 - Jun 20th, 2005, 8:47pm
 
I was wondering the same thing a while ago, and I found this post about it

http://processing.org/discourse/yabb/YaBB.cgi?board=Syntax;action=display;num=1067383998;start=1

It seems like all you have to do is make a class that extends Runnable, and it's threaded. It worked for me at least. Fry, whatever happened to that threading Example? Wink
Re: Multithreading?
Reply #2 - Jun 20th, 2005, 11:05pm
 
yeah, he's still buried somewhere on the todo list..

but this should get you started:

Code:
// in your app:

ThreadThing tt = new ThreadThing(this);
tt.start();


// the thread class:

public class ThreadThing implements Runnable {
Thread thread;

public ThreadThing(PApplet parent) {
parent.registerDispose(this);
}

public void start() {
thread = new Thread(this);
thread.start();
}

public void run() {
// do something threaded here
}

public void stop() {
thread = null;
}

// this will magically be called by the parent once the user hits stop
// this functionality hasn't been tested heavily so if it doesn't work, file a bug
public void dispose() {
stop();
}
}
Re: Multithreading?
Reply #3 - Jun 26th, 2005, 7:44pm
 
Okay I know this is an old thread, but I came up with this simple example for multithreading. It's supposed to be kind of like files being loaded in the background, so these aren't infinite threads. They only live long enough to fill up and then hopefully die out. Do I need to call stop() at the end of run(), or does it happen automatically?

Code:
// MultiThread
// A Boring Threading Example
// by Ryan Alexander (http://onecm.com)

FillThread fillers[] = new FillThread[18];

int filledThreads;
boolean done;


void setup()
{
size(200,200);

initThreads();

noStroke();
}

void draw()
{
background(0);

// Draw //
if(done) fill(255,255,0);
else fill(255);
rect(0,height, 20,(float)filledThreads/fillers.length*-height);

for(int i=0; i<fillers.length; i++) {
if(fillers[i].filled) fill(255,255,0);
else fill(255);
rect(20+i*10, height, 10, height*(fillers[i].level/-100f));
}

// If Everything is filled then reset
if(done) initThreads();
}

void initThreads()
{
done = false;
filledThreads = 0;

for(int i=0; i<fillers.length; i++)
{
fillers[i] = new FillThread(this, (int)random(20,100));
fillers[i].start();
}
}

void threadFilled()
{
if(++filledThreads == fillers.length) done = true;
}


// Thread Class //

public class FillThread implements Runnable
{
Thread thread;

int pauseTime; // Time to wait between loops
int level = 0;
boolean filled;

public FillThread(PApplet parent, int pt)
{
parent.registerDispose(this);

pauseTime = pt;
}

public void start()
{
thread = new Thread(this);
thread.start();
}

public void run()
{
// do something threaded here
while(level<100) {
level++;

// Wait a little (give other threads time to run)
try {
Thread.sleep(pauseTime);
}
catch(InterruptedException e) {}
}
filled = true;
threadFilled();
}

public void stop()
{
thread = null;
}

// this will magically be called by the parent once the user hits stop
// this functionality hasn't been tested heavily so if it doesn't work, file a bug
public void dispose() {
stop();
}
}
Page Index Toggle Pages: 1