# Processing3 problems with using a variable in draw and thread

**URL:** <https://discourse.processing.org/t/processing3-problems-with-using-a-variable-in-draw-and-thread/39856>\
**Category:** Beginners\
**Created:** [November 24, 2022, 1:01pm UTC](https://discourse.processing.org/t/processing3-problems-with-using-a-variable-in-draw-and-thread/39856 "2022-11-24T13:01:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![t00cg](https://avatars.discourse-cdn.com/v4/letter/t/0ea827/32.png) [@t00cg](https://discourse.processing.org/u/t00cg)\
**Post date:** [November 24, 2022, 1:01pm UTC](https://discourse.processing.org/t/processing3-problems-with-using-a-variable-in-draw-and-thread/39856/1 "2022-11-24T13:01:42Z")

</div>

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

```auto

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);
}

```

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [November 24, 2022, 1:50pm UTC](https://discourse.processing.org/t/processing3-problems-with-using-a-variable-in-draw-and-thread/39856/2 "2022-11-24T13:50:24Z")

</div>

Hi @t00cg,

You need to declare to do so …

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

Cheers  
— mnse

```auto
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:

> **[Java Volatile Keyword](https://jenkov.com/tutorials/java-concurrency/volatile.html)**
>
> The Java volatile keyword guarantees variable visibility across threads, meaning reads and writes are visible across threads.

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

---

<div class="post-metadata">

**Author:** ![t00cg](https://avatars.discourse-cdn.com/v4/letter/t/0ea827/32.png) [@t00cg](https://discourse.processing.org/u/t00cg)\
**Post date:** [November 24, 2022, 3:59pm UTC](https://discourse.processing.org/t/processing3-problems-with-using-a-variable-in-draw-and-thread/39856/3 "2022-11-24T15:59:45Z")

</div>

> [@mnse](#):
>
> `volatile`

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 .-)

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [November 24, 2022, 4:33pm UTC](https://discourse.processing.org/t/processing3-problems-with-using-a-variable-in-draw-and-thread/39856/4 "2022-11-24T16:33:53Z")

</div>

OMG! Brainsf\*\*\* … Lange her, dass ich darüber gestolpert bin… 😂 🤣

Viel Spaẞ damit!

Cheers  
— mnse

---

<div class="post-metadata">

**Author:** ![t00cg](https://avatars.discourse-cdn.com/v4/letter/t/0ea827/32.png) [@t00cg](https://discourse.processing.org/u/t00cg)\
**Post date:** [November 24, 2022, 6:19pm UTC](https://discourse.processing.org/t/processing3-problems-with-using-a-variable-in-draw-and-thread/39856/5 "2022-11-24T18:19:09Z")

</div>

wird nächstes jahr anscheinend 30 jahre alt.
