Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello,I\'m a begginer in C++ multi-thread programming, and I need to solve the f

ID: 673634 • Letter: H

Question

Hello,I'm a begginer in C++ multi-thread programming, and I need to solve the following problem:

" Implement a multithreaded program that solve the following issue:

The parking lot of an university has 5 parking spaces. While there are spaces available, there is no priority. When it is full, a queue is formed where teachers have the highest priority, followed by students.
Implement threads student and teacher (8 students and 4 teachers). These threads should run into an infinite loop the following actions:

-Aluno: Parks, studies, and go home.
-Teacher: Parks, teaches, and go home. "

Please, can someone helpe me ?

Explanation / Answer

I can show you how to use thread priority and how to make infinite loop, my sample code would help you to understand this :
Thread Priorities

Used by the thread scheduler to decide when each thread should be allowed to run.

final void setPriority(int level)
final int getPriority()

The value of level must be within the range MIN_PRIORITY and MAX_PRIORITY. Currently these values are 1 and 10 respectively. Default priority is NORM_PRIORITY, which is currently 5. These are defined as final variables within Thread.

class Clicker implements Runnable
{
int click = 0;
Thread t;
private volatile boolean running = true;
public Clicker(int p)
{
t = new Thread(this);
t.setPriority(p);
}
public void run ()
{
while (running)
{
click++;
}
}
public void stop() {
running = false;
}
public void start ()
{
t.start();
}
}
class HiLoPri
{
public static void main (String args [ ])
{
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
Clicker hi = new Clicker (Thread.NORM_PRIORITY + 2);
Clicker lo = new Clicker (Thread.NORM_PRIORITY - 2);
lo.start();
hi.start();
try
{
Thread.sleep(10000);
}
catch (InterruptedException e)
{
System.out.println("Main Thread Interrupted");
}
lo.stop();
hi.stop();
try
{
hi.t.join();
lo.t.join();
}
catch (InterruptedException e)
{
System.out.println("InterruptedException Caught");
}
System.out.println("Low-Priority Thread:" + lo.click);
System.out.println("High-Priority Thread:" + hi.click);
}
}

The output of this program, shown as follows when run under Windows98, indicates that the threads did context switch even though neither voluntarily yielded the CPU nor blocked for I/O. The higher priority thread got approx. 90% of the CPU time.

Low-Priority Thread:4408112

High-Priority Thread:589626904

The output depends on the speed of your CPU and the number of other tasks running in the system.

For infinite Loop >>>
Volatile ensures that the value of running is examined each time the following loop iterates:

while(running)
{
click++;
}