mirror of
https://github.com/Hopiu/rpi-rgb-led-matrix.git
synced 2026-04-27 01:54:42 +00:00
34 lines
769 B
C++
34 lines
769 B
C++
// -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
|
|
#include "thread.h"
|
|
|
|
#include <sched.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <assert.h>
|
|
|
|
void *Thread::PthreadCallRun(void *tobject) {
|
|
reinterpret_cast<Thread*>(tobject)->Run();
|
|
return NULL;
|
|
}
|
|
|
|
Thread::Thread() : started_(false) {}
|
|
Thread::~Thread() {
|
|
if (!started_) return;
|
|
int result = pthread_join(thread_, NULL);
|
|
if (result != 0) {
|
|
fprintf(stderr, "err code: %d %s\n", result, strerror(result));
|
|
}
|
|
}
|
|
|
|
void Thread::Start(int priority) {
|
|
assert(!started_);
|
|
pthread_create(&thread_, NULL, &PthreadCallRun, this);
|
|
|
|
if (priority > 0) {
|
|
struct sched_param p;
|
|
p.sched_priority = priority;
|
|
pthread_setschedparam(thread_, SCHED_FIFO, &p);
|
|
}
|
|
|
|
started_ = true;
|
|
}
|