1

update preemption demo to variable thread num

This commit is contained in:
2022-07-16 00:58:59 +02:00
parent 24cf319b86
commit d1d3714b63
2 changed files with 18 additions and 13 deletions

View File

@ -2,34 +2,37 @@
void PreemptiveLoopThread::run() {
int cnt = 0;
while (true) {
// Basic synchronization by semaphore
sem->p();
kout.setpos(55, this->id);
kout << this->id << ": " << dec << cnt++ << endl;
// Saving + restoring kout position doesn't help much as preemption still occurs,
// only LoopThreads are synchronized, so other output will be disturbed sometimes
// kout.setpos(55, this->id);
// kout << fillw(3) << this->id << fillw(0) << ": " << dec << cnt++ << endl;
sem->v();
}
}
void PreemptiveThreadDemo::run() {
kout << "PreemptiveThreadDemo ====================================" << endl;
kout << "Initializing Semaphore" << endl;
Semaphore* sem = new Semaphore(1); // Create this semaphore on the heap as this thread exits itself,
// so stack allocated objects will be lost
kout << "Allocating LoopThread A" << endl;
Thread* cntA = new PreemptiveLoopThread(0, sem);
kout << "Allocating LoopThread B" << endl;
Thread* cntB = new PreemptiveLoopThread(1, sem);
kout << "Allocating LoopThread C" << endl;
Thread* cntC = new PreemptiveLoopThread(2, sem);
// TODO: Threads disappear, the fewer loop the faster they vanish
// It seems like this happens with exactly 3 or 4 loopthreads
Thread* threads[this->number_of_threads];
kout << "Allocating LoopThreads" << endl;
for (unsigned int i = 0; i < this->number_of_threads; ++i) {
threads[i] = new PreemptiveLoopThread(i, sem);
}
kout << "Adding threads to ready queue" << endl;
scheduler.ready(cntA);
scheduler.ready(cntB);
scheduler.ready(cntC);
for (unsigned int i = 0; i < this->number_of_threads; ++i) {
scheduler.ready(threads[i]);
}
kout << "Exiting main thread" << endl;
scheduler.exit();

View File

@ -24,8 +24,10 @@ class PreemptiveThreadDemo : public Thread {
private:
PreemptiveThreadDemo(const PreemptiveThreadDemo& copy) = delete; // Verhindere Kopieren
unsigned int number_of_threads;
public:
PreemptiveThreadDemo() {
PreemptiveThreadDemo(unsigned int n) : number_of_threads(n) {
kout << "Initialized PreemptiveThreadDemo" << endl;
}