Old Question: What is the difference between SDL_Thread and std::thread?
Hi
What is the difference between these 2? Which should I use and why?
Thanks
Edit:
New question: What is the difference between pthread and SDL_Thread? Which should I use when programming a game with SDL and why?
Edit 2:
After everything so far, apparently std::thread is the best option.
As a follow up question, how can I manage the following issue properly?
Issue: I have objects. Each object has a pointer to a function. When the object is clicked, it calls that function. But I want to call certain functions after a delay. I have come up with 2 solutions:
1) The most intuitive solution is to call those functions as separate thread, but I will probably end with a ton of threads at some point.
2) I could do some gimmicks and call that function every time and check with if's the current frame and subtract from it's given delay 1 for each frame that passes and when it reaches 0, it actually calls that function, but that sounds horribly spaghetti.
I have heard threads are evil when it comes to game development and most games handle the main game in a single thread. Is there another way to handle what I want to do?
SDL_Thread is a platform-independent thread interface, written in C. std::thread is a standard platform-independent thread, obviously written in C++.
You should prefer std::thread.
Thanks for the answer, but why is that? Why should I prefer std::thread over SDL_Thread?
To expand my question, I have tried and failed to use SDL_Thread properly with objects but I thought maybe I am doing something wrong.
Anyhow, I acknowledge the existence of 'pthreads' but I have not delved into them and only know they are in C.
What would be the difference between pthreads and SDL_Threads? Is there any advantage SDL_Thread has over pthread and why?
Those three kind of thread interfaces are doing the same: They create a thread. The differences are how they deal with the provided data/in this case the function.
std::thread is designed to work well with C++ classes, hence it would be wise to use the standard thread.
My suggestion: Usually prefer the standard library except you have a good reason to do otherwise.
Is there any advantage SDL_Thread has over pthread and why?
Mainly, platform-independence. libsdl provides a threading abstraction to facilitate portability.
In the presence of <thread>, the portability concern is moot -- I expect that std::thread is much more portable than SDL's wrapper, and certainly more portable than POSIX threads. Additionally, std::thread is written in C++ with C++ idioms in mind, and because it is standard it is widely used and well understood. Because of these qualities (C++-aware, standard, portable) std::thread (or standard facilities in general) should be preferred when possible.