Let’s think an application that user can determine to draw rectangle at the center of the Component.
class MainComponent : public juce::Component
{
public:
MainComponent()
{
addAndMakeVisible(button);
button.setClickingTogglesState(true);
button.onClick = [this]
{
drawRect = button.getToggleState();
repaint();
};
setSize (600, 400);
}
~MainComponent() override = default;
void paint (juce::Graphics& g) override
{
g.fillAll (getLookAndFeel().findColour (juce::ResizableWindow::backgroundColourId));
if (drawRect)
{
g.setColour(juce::Colours::red);
g.fillRect(getLocalBounds().withSizeKeepingCentre(100, 100));
}
}
void resized() override
{
button.setBounds(getLocalBounds().removeFromTop(30).removeFromRight(50));
}
private:
std::atomic<bool> drawRect = false;
juce::TextButton button{ "Paint" };
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainComponent)
};
My question is, should I wrap drawRect with std::atomic for the thread safety?
If MainComponent is attached to the OpenGLContext, paint() should not be called from main thread?
Yes, it’s very rare that user click the button and toggle the variable during the thread pool is in the paint(). But I’m wondering if I can ignore the corner case.
