Loop does not work with a thread


#1

Hello, I´m currently experimenting with threads and I wrote this simple window loop to test it.

	void Window::thread_windowloop()
{
	static bool wa = true;

	while (wa)
	{
		SDL_Event e;

		while (SDL_PollEvent(&e) != 0)
		{
			if (e.type == SDL_QUIT)
			{
				wa = false;
			}
		}
		
		SDL_RenderClear(renderer);
		SDL_RenderPresent(renderer);

	}
}

When I call this function normally, everything works, but when I call this function with a std::thread, the window freezes. Is there a solution or is it not possible to run this with a thread?


#2

If I’m not mistaken, you can only call the SDL_RenderClear and SDL_RenderPresent functions from the thread that initialized SDL Video with: SDL_InitSubSystem(SDL_INIT_VIDEO). If you initialize everything somewhere else, you’ll need to stop it and start it in that thread again.