Оболочка над WINAPI функциями windows для работы с потоками
//! wrapper
class CriticalSection
{
RTL_CRITICAL_SECTION m_cs;
public:
CriticalSection()
{
memset(&m_cs,0,sizeof(m_cs));
::InitializeCriticalSection(&m_cs);
}
~CriticalSection()
{
::DeleteCriticalSection(&m_cs);
}
void Enter()
{
::EnterCriticalSection(&m_cs);
}
void Leave()
{
::LeaveCriticalSection(&m_cs);
}
};
//! scoped
class Critical
{
CriticalSection& m;
public:
Critical(CriticalSection& cs) : m(cs)
{
m.Enter();
}
~Critical()
{
m.Leave();
}
};
HRESULT SetCurrentThreadPriorityBelowNormal()
{
HANDLE h = ::GetCurrentThread();
if(!h) return E_FAIL;
if(!::SetThreadPriority(h, THREAD_PRIORITY_BELOW_NORMAL))
{
return E_FAIL;
}
return 0;
}
HRESULT SetCurrentThreadPriorityNormal()
{
HANDLE h = ::GetCurrentThread();
if(!h) return E_FAIL;
if(!::SetThreadPriority(h, THREAD_PRIORITY_NORMAL))
{
return E_FAIL;
}
return 0;
}
HRESULT SetCurrentThreadPriorityAboveNormal()
{
HANDLE h = ::GetCurrentThread();
if(!h)
{
return E_FAIL;
}
if(!::SetThreadPriority(h, THREAD_PRIORITY_ABOVE_NORMAL))
{
return E_FAIL;
}
return 0;
}
HANDLE CreateThreadDefault(LPTHREAD_START_ROUTINE thread_proc, void* userdata, bool suspended)
{
DWORD dwCreationFlags = 0;
if(suspended)
{
dwCreationFlags = CREATE_SUSPENDED;
}
HANDLE res = ::CreateThread( NULL, 0, thread_proc, userdata, 0, NULL);
if(!res)
{
throw std::runtime_error("Error CreateThread");
}
return res;
}
//========================================================
|