# OpenGL Render Thread Resizing when Component Resized

**URL:** <https://forum.juce.com/t/opengl-render-thread-resizing-when-component-resized/41944>\
**Category:** General JUCE discussion\
**Created:** [September 28, 2020, 5:30pm UTC](https://forum.juce.com/t/opengl-render-thread-resizing-when-component-resized/41944 "2020-09-28T17:30:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![timart](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/timart/32/1119_2.png) [@timart](https://forum.juce.com/u/timart)\
**Post date:** [September 28, 2020, 5:30pm UTC](https://forum.juce.com/t/opengl-render-thread-resizing-when-component-resized/41944/1 "2020-09-28T17:30:20Z")

</div>

Hello all,

I am writing an OpenGL audio visualizer and using the `OpenGLRenderer` to have a separate render thread for OpenGL. Just as shown in `OpenGLDemo.h` in the JUCE demos, I want to have my OpenGL rendering scaled to fit the `Component` size.

In `OpenGLDemo.h`, in the `OpenGLRenderer::renderOpenGL()` method it uses the following code to set the OpenGL viewport to match the component size:

```auto
auto desktopScale = (float) openGLContext.getRenderingScale();
...
glViewport (0, 0, roundToInt (desktopScale * (float) getWidth()), roundToInt (desktopScale * (float) getHeight()));

```

**Is this a race condition between the JUCE Message Thread and the OpenGL rendering thread on the `Component` width and height data?**

From what I understand, the JUCE Message thread handles things like Component resizing, mouse clicks, etc. The OpenGL thread accesses the Component width and height (in the last line of code above), and since the Message thread could simultaneously be writing to the Component size while the OpenGL thread reads from it, I believe this is a race condition.

If I am correct in my thinking that this is a race condition, what is a good way to deal with this race condition? I thought about locking the OpenGL thread while the Component is being resized and then unlocking after resize has completed. Is asynchronous messaging a better solution? Maybe an atomic boolean flag?

I would assume other people have dealt with this issue and was looking to see if there was a standard solution most people used.

Thanks!

---

<div class="post-metadata">

**Author:** ![PluginPenguin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pluginpenguin/32/10203_2.png) [@PluginPenguin](https://forum.juce.com/u/PluginPenguin)\
**Post date:** [September 29, 2020, 6:34am UTC](https://forum.juce.com/t/opengl-render-thread-resizing-when-component-resized/41944/2 "2020-09-29T06:34:31Z")

</div>

As far as I know the GL Thread holds a message manager lock during the render callback, so it‘s safe to do all this

---

<div class="post-metadata">

**Author:** ![timart](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/timart/32/1119_2.png) [@timart](https://forum.juce.com/u/timart)\
**Post date:** [September 29, 2020, 3:10pm UTC](https://forum.juce.com/t/opengl-render-thread-resizing-when-component-resized/41944/3 "2020-09-29T15:10:03Z")

</div>

Great, it seems this is the case since in `juce_OpenGLContext.cpp` the `renderFrame()` method begins by getting a Message Manager lock:

```auto
bool renderFrame()
{
    MessageManager::Lock::ScopedTryLockType mmLock (messageManagerLock, false);
    ...
}

```

@PluginPenguin Thank you for the help! 🙂
