|
#include "stdafx.h"
#include <stdexcept>
#include "Threads\Thread.h"
namespace Threads
{
Thread::Thread() :
m_stopped(true),
m_thread()
{
}
Thread::~Thread()
{
}
void Thread::Start(unsigned int stackSize/* = 0x10000*/)
{
Windows::AutoCriticalSection lock(m_lock);
if (m_thread != INVALID_HANDLE_VALUE)
{
throw std::runtime_error("The thread is already started.");
}
#ifdef _WIN32_WCE
m_thread = ::CreateThread(0, stackSize, (LPTHREAD_START_ROUTINE)&RunEntry, this, CREATE_SUSPENDED, 0);
#else // _WIN32_WCE
m_thread = (HANDLE)::_beginthreadex(0, stackSize, &RunEntry, this, 0, 0);
#endif // _WIN32_WCE
if (m_thread == INVALID_HANDLE_VALUE)
{
unsigned error = GetLastError();
throw std::runtime_error("Failed to start thread.");
}
// We can start
m_stopped = false;
ResumeThread(m_thread);
}
void Thread::Stop()
{
m_stopped = true;
}
void Thread::Sleep(unsigned int msecs)
{
::Sleep(msecs);
}
void Thread::SetThreadPriority(int priority)
{
Windows::AutoCriticalSection lock(m_lock);
if (m_thread == INVALID_HANDLE_VALUE)
{
throw std::runtime_error("Thread is not running");
}
::SetThreadPriority(m_thread, priority);
}
unsigned __stdcall Thread::RunEntry(void* arglist)
{
Thread& thread = *(Thread*)arglist;
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
{
thread.Run();
}
CoUninitialize();
return 0;
}
} // namespace Threads
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.