code from
http://www.sfml-dev.org/tutorials/2.1/graphics-draw.php:
#include <SFML/Graphics.hpp>
void renderingThread(sf::RenderWindow *window)
{
while (window->isOpen()) {
window->clear();
window->display();
}
}
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "OpenGL");
sf::Thread thread(&renderingThread, &window);
thread.launch();
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
}
return 0;
}
Error when close window:
Pure virtual function called! because window is closed (and may be deleted), but pointer *window in second thread are still used
i change code, add mutex, and now it works fine, but i'm not sure
is this right way to use mutex here?
// main thread
...
if (event.type == sf::Event::Closed) {
mutex.lock();
window.close();
mutex.unlock();
}
...
// second thread
...
mutex.lock();
window->clear();
window->display();
mutex.unlock();
...