Skip to content

Instantly share code, notes, and snippets.

@yni3
Created January 27, 2018 13:12
Show Gist options
  • Save yni3/307207dc3732b59b89e6ec63bbcfb621 to your computer and use it in GitHub Desktop.
Save yni3/307207dc3732b59b89e6ec63bbcfb621 to your computer and use it in GitHub Desktop.
[C++11] thread
/*
SimpleThread.cpp -- implement for SimpleThread.h
Copyright 2018 yni3
License : Public Domain (useful for copy and paste for your project)
*/
#include "SimpleThread.h"
#if defined(_UNIX) || defined(__ANDROID__) || defined(__APPLE__)
#include <pthread.h>
#include <sched.h>
#elif defined(_WIN32)
#include <windows.h>
#endif
using namespace yni3;
SimpleThread::SimpleThread()
:m_pThread(NULL)
, m_priority(NORMAL)
, m_bToFinish(false)
, m_bFinished(false)
{
}
SimpleThread::~SimpleThread()
{
Stop();
}
void SimpleThread::Start()
{
if (m_pThread == NULL) {
//m_pThread = new std::thread((*this));
m_pThread = new std::thread(std::bind(&SimpleThread::operator(),this));
if (m_priority != NORMAL) {
SetPriority(m_priority);
}
}
}
void SimpleThread::Stop()
{
if (m_pThread->joinable())
{
ToFinish();
m_pThread->join();
}
}
bool SimpleThread::IsFinised() const
{
return m_bFinished;
}
void SimpleThread::ToFinish()
{
m_bToFinish = true;
}
bool SimpleThread::isToFinish() const
{
return m_bToFinish;
}
void SimpleThread::SleepMs(long long s)
{
std::this_thread::sleep_for(std::chrono::milliseconds(s));
}
void SimpleThread::SetPriority(PRIORITY p)
{
m_priority = p;
if (m_pThread) {
#if defined(_UNIX) || defined(__ANDROID__) || defined(__APPLE__)
const int max = sched_get_priority_max(SCHED_OTHER);
const int min = sched_get_priority_min(SCHED_OTHER);
const int normal = (max - min) / 2;
int pt = normal;
switch (p) {
case HIGH:
pt = max;
break;
case IDLE:
pt = min;
break;
case NORMAL:
default:
pt = normal;
break;
}
sched_param sch_params;
sch_params.sched_priority = pt;
if (pthread_setschedparam(m_pThread->native_handle(), SCHED_OTHER, &sch_params)) {
assert(false);
}
#elif defined(_WIN32)
int pt = THREAD_PRIORITY_NORMAL;
switch (p) {
case HIGH:
pt = THREAD_PRIORITY_HIGHEST;
break;
case IDLE:
pt = THREAD_PRIORITY_LOWEST;
break;
case NORMAL:
default:
pt = THREAD_PRIORITY_NORMAL;
break;
}
SetThreadPriority(m_pThread->native_handle(), pt);
#else
//system does not supports thread priority!
#endif
}
}
void SimpleThread::operator()()
{
while (!m_bToFinish) {
OnLoop();
}
OnPost();
m_bFinished = true;
}
/*
SimpleThread.h -- [C++11] thread abstruct class. simple use for Inheritance
Copyright 2018 yni3
License : Public Domain (useful for copy and paste for your project)
*/
#pragma once
#ifndef __SIMPLETHREAD
#define __SIMPLETHREAD
#include <thread>
namespace yni3 {
class SimpleThread
{
public:
enum PRIORITY {
HIGH,
NORMAL,
IDLE,
};
SimpleThread();
virtual ~SimpleThread();
//start thread sync
void Start();
//start thread sync
void Stop();
//after OnPost
bool IsFinised() const;
//set thread priority
void SetPriority(PRIORITY);
protected:
virtual void OnLoop() = 0;
virtual void OnPost() = 0;
void SleepMs(long long);
bool isToFinish() const;
void ToFinish();
private:
void operator()();
bool m_bFinished;
bool m_bToFinish;
PRIORITY m_priority;
std::thread* m_pThread;
};
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment