mirror of
https://github.com/HifiExperiments/overte.git
synced 2025-07-23 10:14:25 +02:00
62 lines
No EOL
1.4 KiB
C++
62 lines
No EOL
1.4 KiB
C++
//
|
|
// GenericThread.cpp
|
|
// shared
|
|
//
|
|
// Created by Brad Hefta-Gaub on 8/12/13.
|
|
// Copyright (c) 2013 High Fidelity, Inc. All rights reserved.
|
|
//
|
|
// Generic Threaded or non-threaded processing class
|
|
//
|
|
|
|
#include "GenericThread.h"
|
|
|
|
GenericThread::GenericThread() :
|
|
_stopThread(false),
|
|
_isThreaded(false) // assume non-threaded, must call initialize()
|
|
{
|
|
}
|
|
|
|
GenericThread::~GenericThread() {
|
|
terminate();
|
|
}
|
|
|
|
void GenericThread::initialize(bool isThreaded) {
|
|
_isThreaded = isThreaded;
|
|
if (_isThreaded) {
|
|
pthread_create(&_thread, NULL, GenericThreadEntry, this);
|
|
}
|
|
}
|
|
|
|
void GenericThread::terminate() {
|
|
if (_isThreaded) {
|
|
_stopThread = true;
|
|
pthread_join(_thread, NULL);
|
|
_isThreaded = false;
|
|
}
|
|
}
|
|
|
|
void* GenericThread::threadRoutine() {
|
|
while (!_stopThread) {
|
|
|
|
// override this function to do whatever your class actually does, return false to exit thread early
|
|
if (!process()) {
|
|
break;
|
|
}
|
|
|
|
// In non-threaded mode, this will break each time you call it so it's the
|
|
// callers responsibility to continuously call this method
|
|
if (!_isThreaded) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (_isThreaded) {
|
|
pthread_exit(0);
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
extern "C" void* GenericThreadEntry(void* arg) {
|
|
GenericThread* genericThread = (GenericThread*)arg;
|
|
return genericThread->threadRoutine();
|
|
} |