qff
asked on
SDL: threaded application design
I'm making a game, programmed in C++ with SDL and some use of the boost libraries.
I am aware of the pitfalls of multithreaded application development (at least somewhat aware).
My plan is to have event-handling, drawing and game mechanics running in seperate threads.
I'm thinking about letting the event-handler spawn a new thread to make the appropriate response to a caught event - so the event-handler is not interrupted or halted for too long each time an event occurs.
Should I use SDL_WaitEvent or SDL_PollEvent in this case for checking events?
My question is whether I'm on a wrong trail here (am I doing it completely wrong?).
If you have any suggestions to improve my application design (or if you simply have a better one), I would love to hear them.
I'm currently planning to use SDL threads - would I be better of with boost.thread?
I am aware of the pitfalls of multithreaded application development (at least somewhat aware).
My plan is to have event-handling, drawing and game mechanics running in seperate threads.
I'm thinking about letting the event-handler spawn a new thread to make the appropriate response to a caught event - so the event-handler is not interrupted or halted for too long each time an event occurs.
Should I use SDL_WaitEvent or SDL_PollEvent in this case for checking events?
My question is whether I'm on a wrong trail here (am I doing it completely wrong?).
If you have any suggestions to improve my application design (or if you simply have a better one), I would love to hear them.
I'm currently planning to use SDL threads - would I be better of with boost.thread?
ASKER
Shouldn't that "std::sig_atomic_t" be "volatile"?
The compiler does not know that the variable can be changed from elsewhere and by your code it is lead to think that it is constant after having been set to "0". The compiler does not take the effects multithreading into account by default - you must tell it that the variable can be changed from elsewhere i.e. "volatile std::sig_atomic_t& m_sigQuit;".
Furthermore I believe that the mentioned atomicity of the specific type does not take multithreading into account either.
The compiler does not know that the variable can be changed from elsewhere and by your code it is lead to think that it is constant after having been set to "0". The compiler does not take the effects multithreading into account by default - you must tell it that the variable can be changed from elsewhere i.e. "volatile std::sig_atomic_t& m_sigQuit;".
Furthermore I believe that the mentioned atomicity of the specific type does not take multithreading into account either.
>> Shouldn't that "std::sig_atomic_t" be "volatile"?
A reference is really just a pointer in disguise. The value of the reference never changes but what it references does, and therefore making it volatile is unnecessary.
>> Furthermore I believe that the mentioned atomicity of the specific type does not take multithreading into account either.
The type sig_atomic_t can be read/written atomically in 1 CPU cycle, this is guaranteed by the C++ Standard, it is therefore safe to use in asynchronous code in this matter.
http://www.devx.com/tips/Tip/15212
Also, it's probably worth pointing out that this is an example that it has been implemented with as little code as possible so as to not detract from the point, which is to show the simple usage of boost threads.
A reference is really just a pointer in disguise. The value of the reference never changes but what it references does, and therefore making it volatile is unnecessary.
>> Furthermore I believe that the mentioned atomicity of the specific type does not take multithreading into account either.
The type sig_atomic_t can be read/written atomically in 1 CPU cycle, this is guaranteed by the C++ Standard, it is therefore safe to use in asynchronous code in this matter.
http://www.devx.com/tips/Tip/15212
Also, it's probably worth pointing out that this is an example that it has been implemented with as little code as possible so as to not detract from the point, which is to show the simple usage of boost threads.
>> A reference is really just a pointer in disguise.
http://www.parashift.com/c++-faq-lite/references.html
http://www.parashift.com/c++-faq-lite/references.html
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you for your help ;)
I don't know about better as I've never used SDL but I can tell you that using boost threads is very simple indeed. Below is some example code of spawning and stopping a thread.
Open in new window