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

Author Topic: Issue with RenderWindow and threads  (Read 1378 times)

0 Members and 1 Guest are viewing this topic.

dmfrodrigues

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Issue with RenderWindow and threads
« on: February 19, 2021, 05:55:49 pm »
Computer: Laptop, 8 cores
Environment: Ubuntu 20.04.2 LTS, 64bit, X11 windowing system
SFML: What is available in apt as of now under the name 'libsfml-dev', which is version 2.5.1+dfsg-1build1
(For any more info feel free to ask)

Hello!

I was trying to create a multi-thread application, where three threads are created and each thread creates a single window and manages that window's event loop (except for the main thread which is not responsible for any windows), and then the main thread joins all additional threads and exits, so basically the application waits until all windows are closed. Here is a MWE:

#include <thread>
#include <SFML/Graphics.hpp>
#include <X11/Xlib.h>
#include <stdio.h>

void newWindow(){
    printf("Creating window\n");
    sf::RenderWindow *window = new sf::RenderWindow(sf::VideoMode(400, 400), "A window");
    printf("Created window\n");
    while (window->isOpen()){
        sf::Event event;
        while (window->pollEvent(event)){
            switch(event.type){
                case sf::Event::Closed: window->close(); break;
                default: break;
            }
        }
        window->display();
    }
    delete window;
}

int main(){
    XInitThreads();
    std::thread t1(newWindow);
    std::thread t2(newWindow);
    std::thread t3(newWindow);
    t1.join();
    t2.join();
    t3.join();
    return 0;
}
 

Real simple, actually. Compiled it with
g++ -Wall main.cpp -lsfml-graphics -lsfml-window -lsfml-system -pthread -lX11
as the law demands.

I was expecting to see three windows open up, and have the following on the console:
Creating window
Creating window
Creating window
Created window
Created window
Created window

or something along those lines, as print order might change.

However, what I get is often only three times Creating window and the app is stuck (and the threads probably aren't going away, as my CPU jumps from a stable 5% to a stable 50%).
Once I got
Creating window
Creating window
Creating window
Failed to create input context for window -- TextEntered event won't be able to return unicode
Created window

And once I closed the only open window, the app is stuck.

And when I comment out t3, I sometimes get it right but once I got
Creating window
Creating window
malloc(): smallbin double linked list corrupted
Aborted (core dumped)

In this case it exited with an error.

How come the application gets stuck? I truly hope this is not some sort of rookie mistake, I've looked through the forum and the docs for a whole day and couldn't find the issue. From the console prints I can say that the issue is likely in the RenderWindow constructor.

Any input is appretiated

EDIT:

Added a global mutex that I lock before constructing RenderWindow, and unlock after constructing RenderWindow, and so far the issue has not surfaced. I wonder if there's some simultaneous access issue that RenderWindow is not handling (like asking X11 to create a new window or something like that)
« Last Edit: February 19, 2021, 06:23:10 pm by dmfrodrigues »