# Thread issue with while (true)

**URL:** <https://discourse.processing.org/t/thread-issue-with-while-true/16410>\
**Category:** Coding Questions\
**Created:** [December 15, 2019, 1:12am UTC](https://discourse.processing.org/t/thread-issue-with-while-true/16410 "2019-12-15T01:12:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![molo32](https://avatars.discourse-cdn.com/v4/letter/m/a87d85/32.png) [@molo32](https://discourse.processing.org/u/molo32)\
**Post date:** [December 15, 2019, 1:12am UTC](https://discourse.processing.org/t/thread-issue-with-while-true/16410/1 "2019-12-15T01:12:29Z")

</div>

when I run a while (true) inside a thread  
It only works if the condition play = 1 is met in the beginning.

but if play = 0 in the beginning, in the future of the program when play == 1 what is inside while is not running, I thought it should be executed since while (true) is waiting for instructions.

How do I make while (true) work at any time and not just in the beginning.

```auto
//variable init
play=0

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

void holo(){
  while(true){
    if (play==1){
      //...//it does not run because play 0 was initialized with 0
 //but it does not run in the future when play = 1;
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [December 15, 2019, 1:27am UTC](https://discourse.processing.org/t/thread-issue-with-while-true/16410/2 "2019-12-15T01:27:02Z")

</div>

Use **delay()** for functions continuously running in another **thread()**:  
[Processing.org/reference/delay\_.html](http://Processing.org/reference/delay_.html)  
[Processing.org/reference/thread\_.html](http://Processing.org/reference/thread_.html)

```auto
int play;
color c;

void setup() {
  size(200, 150);
  frameRate(4);
  thread("holo");
}

void draw() {
  play = round(random(1));
  background(c);
  getSurface().setTitle("Frames: " + frameCount);
}

void holo() {
  while (true) {
    if (play == 1) c = (color) random(#000000);
    delay(1);
  }
}

```

Alternatively, declare the conditional variable as `volatile`:

```auto
volatile int play;
color c;

void setup() {
  size(200, 150);
  frameRate(4);
  thread("holo");
}

void draw() {
  play = round(random(1));
  background(c);
  getSurface().setTitle("Frames: " + frameCount);
}

void holo() {
  while (true) {
    if (play == 1) c = (color) random(#000000);
    //delay(1);
  }
}

```
