-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
178 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Copyright (C) 2015,2016 by Jonathan Naylor G4KLX | ||
* | ||
* 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() | ||
{ | ||
m_handle = ::CreateThread(NULL, 0, &helper, this, 0, NULL); | ||
|
||
return m_handle != NULL; | ||
} | ||
|
||
|
||
void CThread::wait() | ||
{ | ||
::WaitForSingleObject(m_handle, INFINITE); | ||
|
||
::CloseHandle(m_handle); | ||
} | ||
|
||
|
||
DWORD CThread::helper(LPVOID arg) | ||
{ | ||
CThread* p = (CThread*)arg; | ||
|
||
p->entry(); | ||
|
||
return 0UL; | ||
} | ||
|
||
void CThread::sleep(unsigned int ms) | ||
{ | ||
::Sleep(ms); | ||
} | ||
|
||
#else | ||
|
||
CThread::CThread() : | ||
m_thread() | ||
{ | ||
} | ||
|
||
CThread::~CThread() | ||
{ | ||
} | ||
|
||
bool CThread::run() | ||
{ | ||
return ::pthread_create(&m_thread, NULL, helper, this) == 0; | ||
} | ||
|
||
|
||
void CThread::wait() | ||
{ | ||
::pthread_join(m_thread, NULL); | ||
} | ||
|
||
|
||
void* CThread::helper(void* arg) | ||
{ | ||
CThread* p = (CThread*)arg; | ||
|
||
p->entry(); | ||
|
||
return NULL; | ||
} | ||
|
||
void CThread::sleep(unsigned int ms) | ||
{ | ||
::usleep(ms * 1000); | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright (C) 2015,2016 by Jonathan Naylor G4KLX | ||
* | ||
* 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. | ||
*/ | ||
|
||
#if !defined(THREAD_H) | ||
#define THREAD_H | ||
|
||
#if defined(_WIN32) || defined(_WIN64) | ||
#include <windows.h> | ||
#else | ||
#include <pthread.h> | ||
#endif | ||
|
||
class CThread | ||
{ | ||
public: | ||
CThread(); | ||
virtual ~CThread(); | ||
|
||
virtual bool run(); | ||
|
||
virtual void entry() = 0; | ||
|
||
virtual void wait(); | ||
|
||
static void sleep(unsigned int ms); | ||
|
||
private: | ||
#if defined(_WIN32) || defined(_WIN64) | ||
HANDLE m_handle; | ||
#else | ||
pthread_t m_thread; | ||
#endif | ||
|
||
#if defined(_WIN32) || defined(_WIN64) | ||
static DWORD __stdcall helper(LPVOID arg); | ||
#else | ||
static void* helper(void* arg); | ||
#endif | ||
}; | ||
|
||
#endif |