New API for sf::Thread (more flexible)

git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1685 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
LaurentGom 2010-11-20 13:00:19 +00:00
parent 7ebf2f1bbb
commit e73d274d86
8 changed files with 263 additions and 105 deletions

View file

@ -31,6 +31,10 @@
#include <SFML/System/Sleep.hpp>
#include <SFML/System/Err.hpp>
#ifdef _MSC_VER
#pragma warning(disable : 4355) // 'this' used in base member initializer list
#endif
////////////////////////////////////////////////////////////
// Private data
@ -44,6 +48,7 @@ namespace sf
{
////////////////////////////////////////////////////////////
SoundRecorder::SoundRecorder() :
myThread (&SoundRecorder::Record, this),
mySampleRate (0),
myIsCapturing(false)
{
@ -97,7 +102,7 @@ void SoundRecorder::Start(unsigned int sampleRate)
// Start the capture in a new thread, to avoid blocking the main thread
myIsCapturing = true;
Launch();
myThread.Launch();
}
}
@ -107,7 +112,7 @@ void SoundRecorder::Stop()
{
// Stop the capturing thread
myIsCapturing = false;
Wait();
myThread.Wait();
}
@ -142,7 +147,7 @@ void SoundRecorder::OnStop()
////////////////////////////////////////////////////////////
void SoundRecorder::Run()
void SoundRecorder::Record()
{
while (myIsCapturing)
{

View file

@ -31,11 +31,16 @@
#include <SFML/System/Sleep.hpp>
#include <SFML/System/Err.hpp>
#ifdef _MSC_VER
#pragma warning(disable : 4355) // 'this' used in base member initializer list
#endif
namespace sf
{
////////////////////////////////////////////////////////////
SoundStream::SoundStream() :
myThread (&SoundStream::Stream, this),
myIsStreaming (false),
myChannelsCount (0),
mySampleRate (0),
@ -97,7 +102,7 @@ void SoundStream::Play()
// Start updating the stream in a separate thread to avoid blocking the application
mySamplesProcessed = 0;
myIsStreaming = true;
Launch();
myThread.Launch();
}
@ -113,7 +118,7 @@ void SoundStream::Stop()
{
// Wait for the thread to terminate
myIsStreaming = false;
Wait();
myThread.Wait();
}
@ -156,7 +161,7 @@ void SoundStream::SetPlayingOffset(float timeOffset)
// Restart streaming
mySamplesProcessed = static_cast<unsigned int>(timeOffset * mySampleRate * myChannelsCount);
myIsStreaming = true;
Launch();
myThread.Launch();
}
@ -185,7 +190,7 @@ bool SoundStream::GetLoop() const
////////////////////////////////////////////////////////////
void SoundStream::Run()
void SoundStream::Stream()
{
// Create the buffers
ALCheck(alGenBuffers(BuffersCount, myBuffers));

View file

@ -25,6 +25,7 @@ set(SRC
${INCROOT}/String.hpp
${SRCROOT}/Thread.cpp
${INCROOT}/Thread.hpp
${INCROOT}/Thread.inl
${SRCROOT}/ThreadLocal.cpp
${INCROOT}/ThreadLocal.hpp
${INCROOT}/ThreadLocalPtr.hpp

View file

@ -29,42 +29,19 @@
#if defined(SFML_SYSTEM_WINDOWS)
#include <SFML/System/Win32/ThreadImpl.hpp>
#else
#include <SFML/System/Unix/ThreadImpl.hpp>
#endif
namespace sf
{
////////////////////////////////////////////////////////////
Thread::Thread() :
myThreadImpl(NULL),
myFunction (NULL),
myUserData (NULL)
{
}
////////////////////////////////////////////////////////////
Thread::Thread(Thread::FuncType function, void* userData) :
myThreadImpl(NULL),
myFunction (function),
myUserData (userData)
{
}
////////////////////////////////////////////////////////////
Thread::~Thread()
{
Wait();
delete myFunction;
}
@ -72,18 +49,18 @@ Thread::~Thread()
void Thread::Launch()
{
Wait();
myThreadImpl = new priv::ThreadImpl(this);
myImpl = new priv::ThreadImpl(this);
}
////////////////////////////////////////////////////////////
void Thread::Wait()
{
if (myThreadImpl)
if (myImpl)
{
myThreadImpl->Wait();
delete myThreadImpl;
myThreadImpl = NULL;
myImpl->Wait();
delete myImpl;
myImpl = NULL;
}
}
@ -91,11 +68,11 @@ void Thread::Wait()
////////////////////////////////////////////////////////////
void Thread::Terminate()
{
if (myThreadImpl)
if (myImpl)
{
myThreadImpl->Terminate();
delete myThreadImpl;
myThreadImpl = NULL;
myImpl->Terminate();
delete myImpl;
myImpl = NULL;
}
}
@ -103,8 +80,7 @@ void Thread::Terminate()
////////////////////////////////////////////////////////////
void Thread::Run()
{
if (myFunction)
myFunction(myUserData);
myFunction->Run();
}
} // namespace sf