Processing3 problems with using a variable in draw and thread

hi there

in my opinion, x should be counted till 20 and then reseted in the thread.

but it is not happening.

can someone help me? thanks for any help

t00cg


public int x = 0;

void setup() {

  thread("threadcode");
} 

void threadcode() {
  while(true) {   
 if (x>20) x=0; } 
  
}

void draw() {
  clear();
   x += 1;
  text(" "+x,20,20);
}

Hi @t00cg,

You need to declare to do so …

The Java volatile keyword guarantees visibility of changes to variables across threads.

Cheers
— mnse

volatile int x = 0;

void setup() {
  thread("threadcode");
} 

void threadcode() {
  while (true) {   
    if (x>20) x=0;
  }
}

void draw() {
  clear();
  x += 1;
  text(" "+x, 20, 20);
}

further info:

PS: But I do not recommend to do it the way you implemented it … :slight_smile:

1 Like

thank you very much. of course you are right. never. it is only an example that shows the problem.

i am writing a compiling from brainfuckconsole74.ch (a brainfuck videoconsole) to processing.

https://www.brainfuckconsole74.ch

and so i need a thread for processing the data and every 40ms to draw routine independent from it the first 256 cells (which are also the videomemory). and so i only want to draw every 40ms and i had to wait in the processing routing till the next draw routine is coming up or if it was there once or several time to draw at once.

and i am not yet sure if this is the simplest implementation. but it is one. and will be different in c++ or csharp .-) no drawroutine like this.

but you are right. never split a counter over several threads. at the end i need only an if condition in the thread process to go draw in the draw process.

but anyway you helped a lot !!!

another point for the coders* heaven .-)

2 Likes

OMG! Brainsf*** … Lange her, dass ich darüber gestolpert bin… :joy: :rofl:

Viel Spaẞ damit!

Cheers
— mnse

wird nächstes jahr anscheinend 30 jahre alt.