LaurentGomila/SFML — pull request #485 by xv8m4g3r (2013-10-18 13:37:02+00:00)
Title: Added a non-blocking poll() function to Thread.

The new function "bool Thread::poll()" returns true if the thread is still running, false otherwise (either it finished, or something major happened and it never started!).

Only known (possible) issue, is that the *nix version allows you to poll() the current thread, the win32 one fails an assert() if you do. Of course doing so is dumb anyway (of course the current thread is running!).

I was fed up of having to do this kind of checking manually with my own Mutex/bool pairs for half the threads I created, so I hope someone else finds it useful before we all stop using it and just use std::thread. I haven't found a way (portable to things other than glibc at least) to do this 'neatly' on *nix, so it uses a new bool (m_isRunning, and m_isActive was renamed to m_isCreated) in ThreadImpl and a Mutex to lock it. Windows however was much easier as you can set a timeout of zero on WaitForSingleObject(), so it just uses that.

I've done basic tests that everything works as anticipated on Linux(gcc on Ubuntu x64) and Windows(VS2012 on windows7 x86), like all good code, there will probably be bugs I haven't spotted, and I have not tested it at all on OSX.

Apologies if I've done something horribly horribly wrong or bad, or it plain doesn't fit here. But like I said, I just hope someone out there other than me finds it useful.