NXDNClients/NXDNReflector/Thread.cpp

108 lines
1.7 KiB
C++
Raw Normal View History

2018-03-13 18:16:48 -04:00
/*
2020-10-05 08:20:18 -04:00
* Copyright (C) 2015,2016,2020 by Jonathan Naylor G4KLX
2018-03-13 18:16:48 -04:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "Thread.h"
#if defined(_WIN32) || defined(_WIN64)
CThread::CThread() :
m_handle()
{
}
CThread::~CThread()
{
}
bool CThread::run()
{
2020-10-05 08:20:18 -04:00
m_handle = ::CreateThread(NULL, 0, &helper, this, 0, NULL);
2018-03-13 18:16:48 -04:00
2020-10-05 08:20:18 -04:00
return m_handle != NULL;
2018-03-13 18:16:48 -04:00
}
void CThread::wait()
{
2020-10-05 08:20:18 -04:00
::WaitForSingleObject(m_handle, INFINITE);
2018-03-13 18:16:48 -04:00
2020-10-05 08:20:18 -04:00
::CloseHandle(m_handle);
2018-03-13 18:16:48 -04:00
}
DWORD CThread::helper(LPVOID arg)
{
2020-10-05 08:20:18 -04:00
CThread* p = (CThread*)arg;
2018-03-13 18:16:48 -04:00
2020-10-05 08:20:18 -04:00
p->entry();
2018-03-13 18:16:48 -04:00
2020-10-05 08:20:18 -04:00
return 0UL;
2018-03-13 18:16:48 -04:00
}
void CThread::sleep(unsigned int ms)
{
::Sleep(ms);
}
#else
#include <unistd.h>
CThread::CThread() :
m_thread()
{
}
CThread::~CThread()
{
}
bool CThread::run()
{
2020-10-05 08:20:18 -04:00
return ::pthread_create(&m_thread, NULL, helper, this) == 0;
2018-03-13 18:16:48 -04:00
}
void CThread::wait()
{
2020-10-05 08:20:18 -04:00
::pthread_join(m_thread, NULL);
2018-03-13 18:16:48 -04:00
}
void* CThread::helper(void* arg)
{
2020-10-05 08:20:18 -04:00
CThread* p = (CThread*)arg;
2018-03-13 18:16:48 -04:00
2020-10-05 08:20:18 -04:00
p->entry();
2018-03-13 18:16:48 -04:00
2020-10-05 08:20:18 -04:00
return NULL;
2018-03-13 18:16:48 -04:00
}
void CThread::sleep(unsigned int ms)
{
2020-10-05 08:20:18 -04:00
struct timespec ts;
ts.tv_sec = ms / 1000U;
ts.tv_nsec = (ms % 1000U) * 1000000U;
::nanosleep(&ts, NULL);
2018-03-13 18:16:48 -04:00
}
#endif
2020-10-05 08:20:18 -04:00