# Flickering background when updating img.pixels\[\] in another thread

**URL:** https://discourse.processing.org/t/flickering-background-when-updating-img-pixels-in-another-thread/5710
**Category:** Coding Questions
**Created:** [November 19, 2018, 1:48pm UTC](https://discourse.processing.org/t/flickering-background-when-updating-img-pixels-in-another-thread/5710 "2018-11-19T13:48:05Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![companje](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/companje/32/2418_2.png) [@companje](https://discourse.processing.org/u/companje)
#### Post date: [November 19, 2018, 1:48pm UTC](https://discourse.processing.org/t/flickering-background-when-updating-img-pixels-in-another-thread/5710/1 "2018-11-19T13:48:05Z")

</div>

Hello,

When I’m updating the **pixels** of an PImage in a different **thread** this somehow effects the **background** of my main (P3D) sketch (on both Mac and Window). Even when the image is not bound as a texture. Any idea?

```auto
PImage img;
boolean loading;

void setup() {
  size(1200, 1200, P3D);
  img = createImage(2048, 1024, RGB);
}

void draw() {
  background(0);

  if (!loading) {
    thread("loadFrame");
  }
}

void loadFrame() {
  loading=true;

  img.loadPixels();
  for (int i=0; i<img.width*img.height; i++) {
    img.pixels[i] = color(i%255, 255, 255); //// UPDATE: calling color() inside the thread was causing it.
  }
  img.updatePixels();

  loading=false;
}

```

 ![48710488-cd1e2b00-ec08-11e8-8378-4ee24d757d45](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/40ed0e58e334dad8cf823a8a1bd6041037150f80.gif)

---

<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: [November 19, 2018, 2:00pm UTC](https://discourse.processing.org/t/flickering-background-when-updating-img-pixels-in-another-thread/5710/2 "2018-11-19T14:00:14Z")

</div>

Some glitches here & there are expected if we mutate _pixels[]_ in a separate Thread at the same time another 1 displays it. 🙄

However, much worse than that is concurrently use any methods related to `color` under diff. threads! 😨

Main “Animation” Thread is invoking **background()** while the other Thread is invoking **color()**! 😬

As a workaround, you can **createGraphics()** under the other Thread and invoke **color()** via that PGraphics. 💡

Or just don’t use **color()** at all. You can use bitshifting operations instead. 😸

---

<div class="post-metadata">

### Author: ![companje](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/companje/32/2418_2.png) [@companje](https://discourse.processing.org/u/companje)
#### Post date: [November 19, 2018, 2:10pm UTC](https://discourse.processing.org/t/flickering-background-when-updating-img-pixels-in-another-thread/5710/4 "2018-11-19T14:10:11Z")

</div>

Cool, thanks! The **color()** function was causing it.

bitshifting instead of using color() solves the issue:  
`img.pixels[i] = i%255 << 16 | 255 << 8 | 255;`

note that I was not accessing pixels[] but **img.pixels[]**.
