Added an assert in Thread::Wait() if the thread tries to wait for itself

This commit is contained in:
Laurent Gomila 2011-06-14 17:49:33 +02:00
parent d497401e3a
commit 5469eaa8c0
3 changed files with 10 additions and 1 deletions

View File

@ -28,6 +28,7 @@
#include <SFML/System/Unix/ThreadImpl.hpp> #include <SFML/System/Unix/ThreadImpl.hpp>
#include <SFML/System/Thread.hpp> #include <SFML/System/Thread.hpp>
#include <iostream> #include <iostream>
#include <cassert>
namespace sf namespace sf
@ -49,7 +50,10 @@ myIsActive(true)
void ThreadImpl::Wait() void ThreadImpl::Wait()
{ {
if (myIsActive) if (myIsActive)
{
assert(pthread_equal(pthread_self(), myThread) == 0); // A thread cannot wait for itself!
pthread_join(myThread, NULL); pthread_join(myThread, NULL);
}
} }

View File

@ -28,6 +28,7 @@
#include <SFML/System/Win32/ThreadImpl.hpp> #include <SFML/System/Win32/ThreadImpl.hpp>
#include <SFML/System/Thread.hpp> #include <SFML/System/Thread.hpp>
#include <SFML/System/Err.hpp> #include <SFML/System/Err.hpp>
#include <cassert>
#include <process.h> #include <process.h>
@ -38,7 +39,7 @@ namespace priv
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
ThreadImpl::ThreadImpl(Thread* owner) ThreadImpl::ThreadImpl(Thread* owner)
{ {
myThread = reinterpret_cast<HANDLE>(_beginthreadex(NULL, 0, &ThreadImpl::EntryPoint, owner, 0, NULL)); myThread = reinterpret_cast<HANDLE>(_beginthreadex(NULL, 0, &ThreadImpl::EntryPoint, owner, 0, &myThreadId));
if (!myThread) if (!myThread)
Err() << "Failed to create thread" << std::endl; Err() << "Failed to create thread" << std::endl;
@ -57,7 +58,10 @@ ThreadImpl::~ThreadImpl()
void ThreadImpl::Wait() void ThreadImpl::Wait()
{ {
if (myThread) if (myThread)
{
assert(myThreadId != GetCurrentThreadId()); // A thread cannot wait for itself!
WaitForSingleObject(myThread, INFINITE); WaitForSingleObject(myThread, INFINITE);
}
} }

View File

@ -87,6 +87,7 @@ private :
// Member data // Member data
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
HANDLE myThread; ///< Win32 thread handle HANDLE myThread; ///< Win32 thread handle
unsigned int myThreadId; ///< Win32 thread identifier
}; };
} // namespace priv } // namespace priv