Hello everyone,
I'm currently creating a simple project (guess maybe you could call it an engine), for my personal use, that i could reuse in my future applications. So i have thought, that it's a perfect time to start learning some multithreading, and to make the whole project thread-safe.
While working on this, i am using ChatGPT for help.
Basically, Chat has told me, that OpenGL functions that SFML calls internally are not thread-safe, and that loading a sf::Texture object from file is not thread safe also, and loading it, in a separate thread, might cause undefined behavior - because of calls to the OpenGL functions and context switching.
In some of my previous projects i had already used separate thread for loading textures from file, while having a loading screen. I have never had any problems with it, and the loading has succeeded every time (but i have never tested it outside my PC).
The Chat, has also proposed to me a solution to this - he said, that i should load a sf::Image object from file in a separate thread, but then, in a main thread use loadTextureFromImage - in his opinion it would fix this problem.
I'm not sure i can always trust chat with this - especially since i have never had such a problem.
So my question is, is A.I. right about that?
Should i not use a separate thread that loads textures, is it unsafe?
If so, then what are other limitations of multithreading in SFML that i should be aware of?
Is Chat's potential fix to the problem good?
While browsing SFML's texture code on the GitHub, i have found a
comment, that says:
"Force an OpenGL flush, so that the texture will appear updated in all context immediately (solves problems in multi-threaded apps)"
I have never encountered an issue with this approach, but I'm concerned that it might not work universally, and since i have not encountered any reproducible errors i don't have any example code to share.
Thanks in advance for you help and advice!