Processing3 problems with using a variable in draw and thread

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