Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Multi-threading tutorial example does not work  (Read 1193 times)

0 Members and 1 Guest are viewing this topic.

UndergroundCoding

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Multi-threading tutorial example does not work
« on: July 08, 2018, 08:23:40 pm »
Hi guys,

I am trying to create a rendering thread where the program would be able to call all the draw/clear/display functions. The main idea is to separate the window drawing mechanisms from the window event handler, exactly as explained in the tutorial:
https://www.sfml-dev.org/tutorials/2.5/graphics-draw.php#drawing-from-threads

However, the sample code provided in the above link does not work. I have substituted the `sf::Thread` class for the recommended `std::thread`, however I get the exact same problem when trying to run the code.

Example code:
Quote
#include <SFML/Graphics.hpp>
#include <iostream>
#include <thread>

void render(sf::RenderWindow* window)
{
    sf::CircleShape shape(100.f);
    shape.setFillColor(sf::Color::Green);

   
    while(window->isOpen()) {
        window->clear();
        window->draw(shape);
        window->display();
    }   

    return;
}

int main() {
    sf::RenderWindow window(sf::VideoMode(800,600), "OpenGL");
    window.setActive(false);

    std::thread eventThread(render, &window);

    while(window.isOpen()) {
        sf::Event event;

        while(window.pollEvent(event)) {
            if(event.type == sf::Event::Closed)
                window.close();
        }
    }   

    eventThread.join();

    return 1;
}

Where I get the following error:

Quote
Setting vertical sync not supported
XIO:  fatal IO error 11 (Resource temporarily unavailable) on X server ":1"
      after 126 requests (126 known processed) with 0 events remaining.

The first time I run the executable after compiling the program runs, draws the green circle, but will not respond to my close event. After that I get the above error. The number of requests changes each time I run the executable.

I have attempted to use `std::mutex` to avoid race conditions, and despite getting rid of the runtime error, the program is still unresponsive after rendering the drawing.

Can someone please shed some light into what I am doing wrong?

UndergroundCoding

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Multi-threading tutorial example does not work
« Reply #1 on: July 08, 2018, 09:08:26 pm »
Upon further examination and debugging, I have found the following issues:

When adding mutexes to stop race condition, the condition improves. In my previous attempt I had forgotten to unlock the mutex after exiting loops. Although this lead to the program working as expected, it still crashed after a couple of seconds running. The error suggested the use of XInitThreads.

The program is now running as expected after the following steps:
I installed the libX11 package (Fedora):
Quote
sudo dnf install libX11-devel.x86_64 -y

Added the library to the code
Quote
#include <X11/Xlib.h>

Added the XInitThreads() function call before creating a new thread
Quote
int main() {
    // ...
    XInitThreads();
    std::thread eventThread(/*..*/);
    // rest of code...
}

Compiled with the additional flag
Quote
-lX11

This seems to have worked, even after I removed all the mutexes, although I am not entirely sure why.

I will do some further research on the what XInitThreads does and will report back with further details.

 

anything