update preemption demo to variable thread num
This commit is contained in:
@ -2,34 +2,37 @@
|
|||||||
|
|
||||||
void PreemptiveLoopThread::run() {
|
void PreemptiveLoopThread::run() {
|
||||||
int cnt = 0;
|
int cnt = 0;
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
// Basic synchronization by semaphore
|
// Basic synchronization by semaphore
|
||||||
sem->p();
|
sem->p();
|
||||||
|
|
||||||
kout.setpos(55, this->id);
|
// Saving + restoring kout position doesn't help much as preemption still occurs,
|
||||||
kout << this->id << ": " << dec << cnt++ << endl;
|
// 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();
|
sem->v();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void PreemptiveThreadDemo::run() {
|
void PreemptiveThreadDemo::run() {
|
||||||
kout << "PreemptiveThreadDemo ====================================" << endl;
|
|
||||||
kout << "Initializing Semaphore" << endl;
|
kout << "Initializing Semaphore" << endl;
|
||||||
Semaphore* sem = new Semaphore(1); // Create this semaphore on the heap as this thread exits itself,
|
Semaphore* sem = new Semaphore(1); // Create this semaphore on the heap as this thread exits itself,
|
||||||
// so stack allocated objects will be lost
|
// so stack allocated objects will be lost
|
||||||
|
|
||||||
kout << "Allocating LoopThread A" << endl;
|
// TODO: Threads disappear, the fewer loop the faster they vanish
|
||||||
Thread* cntA = new PreemptiveLoopThread(0, sem);
|
// It seems like this happens with exactly 3 or 4 loopthreads
|
||||||
kout << "Allocating LoopThread B" << endl;
|
Thread* threads[this->number_of_threads];
|
||||||
Thread* cntB = new PreemptiveLoopThread(1, sem);
|
kout << "Allocating LoopThreads" << endl;
|
||||||
kout << "Allocating LoopThread C" << endl;
|
for (unsigned int i = 0; i < this->number_of_threads; ++i) {
|
||||||
Thread* cntC = new PreemptiveLoopThread(2, sem);
|
threads[i] = new PreemptiveLoopThread(i, sem);
|
||||||
|
}
|
||||||
|
|
||||||
kout << "Adding threads to ready queue" << endl;
|
kout << "Adding threads to ready queue" << endl;
|
||||||
scheduler.ready(cntA);
|
for (unsigned int i = 0; i < this->number_of_threads; ++i) {
|
||||||
scheduler.ready(cntB);
|
scheduler.ready(threads[i]);
|
||||||
scheduler.ready(cntC);
|
}
|
||||||
|
|
||||||
kout << "Exiting main thread" << endl;
|
kout << "Exiting main thread" << endl;
|
||||||
scheduler.exit();
|
scheduler.exit();
|
||||||
|
|||||||
@ -24,8 +24,10 @@ class PreemptiveThreadDemo : public Thread {
|
|||||||
private:
|
private:
|
||||||
PreemptiveThreadDemo(const PreemptiveThreadDemo& copy) = delete; // Verhindere Kopieren
|
PreemptiveThreadDemo(const PreemptiveThreadDemo& copy) = delete; // Verhindere Kopieren
|
||||||
|
|
||||||
|
unsigned int number_of_threads;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
PreemptiveThreadDemo() {
|
PreemptiveThreadDemo(unsigned int n) : number_of_threads(n) {
|
||||||
kout << "Initialized PreemptiveThreadDemo" << endl;
|
kout << "Initialized PreemptiveThreadDemo" << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user