Skip to content
Snippets Groups Projects
Commit e01c00e3 authored by qwertzniki6's avatar qwertzniki6
Browse files

Refractored code to adhere C# coding conventions

parent 438b920c
No related branches found
No related tags found
No related merge requests found
......@@ -14,8 +14,7 @@ namespace ConsoleApplication1
// Constructor which sets the car id and increments ++ the carId for the next object
public Car()
{
thisCarId = carId;
carId++;
thisCarId = carId++;
Console.WriteLine("car created. id = " + thisCarId);
}
......
......@@ -6,57 +6,57 @@ namespace ConsoleApplication1
{
public class Consumer
{
private Buffer b;
private Random r;
private Boolean isAsleep;
private static Boolean wakeConsumersUp;
private Buffer _buffer;
private Random _random;
private Boolean _isAsleep;
private static Boolean _wakeConsumersUp;
public Consumer(Buffer b)
public Consumer(Buffer buffer)
{
this.b = b;
r = new Random();
_buffer = buffer;
_random = new Random();
}
public void consume()
public void Consume()
{
while (true)
{
//if isAsleep = true: method checks if wakeConsumersUp has been set to true every 50ms
if (isAsleep)
if (_isAsleep)
{
Console.WriteLine("consumer asleep...");
Thread.Sleep(50);
//if wakeConsumersUp = true is asAsleep is set to true and if(isAsleep) "loop" is exited on next interation
if (wakeConsumersUp)
if (_wakeConsumersUp)
{
Console.WriteLine("consumer was woken up");
isAsleep = false;
_isAsleep = false;
}
}
//if consumer isnt asleep: consumer gets mutex from buffer, wakes up producers if buffer is full and removes cars if consumer is not empty
else
{
Thread.Sleep(r.Next(500, 1500));
Mutex m = b.getMutex();
Thread.Sleep(_random.Next(500, 1500));
Mutex m = _buffer.getMutex();
m.WaitOne();
if (b.full())
if (_buffer.full())
{
Producer.wakeUp();
}
if (!b.empty())
if (!_buffer.empty())
{
Car c = b.pop();
Car c = _buffer.pop();
Console.WriteLine("car pulled out of parking space. car id = " + c.getThisCarId());
}
//if buffer is empty: consumers are set to sleep, own instance is set to sleep
else
{
wakeConsumersUp = false;
isAsleep = true;
_wakeConsumersUp = false;
_isAsleep = true;
}
m.ReleaseMutex();
......@@ -66,9 +66,9 @@ namespace ConsoleApplication1
}
//this consumer instance can be woken up by calling this method
public static void wakeUp()
public static void WakeUp()
{
wakeConsumersUp = true;
_wakeConsumersUp = true;
}
}
......
......@@ -43,7 +43,7 @@ namespace ConsoleApplication1
if (b.empty())
{
Consumer.wakeUp();
Consumer.WakeUp();
}
if (!b.full())
......
......@@ -37,7 +37,7 @@ namespace ConsoleApplication1
Consumer c = new Consumer(b);
//create new Thread and start it
Thread newThread = new Thread(c.consume);
Thread newThread = new Thread(c.Consume);
newThread.Start();
}
......
No preview for this file type
No preview for this file type
......@@ -5,3 +5,9 @@ C:\Users\Home\RiderProjects\ConsoleApplication1\ConsoleApplication1\obj\Debug\Co
C:\Users\Home\RiderProjects\ConsoleApplication1\ConsoleApplication1\obj\Debug\ConsoleApplication1.csproj.CoreCompileInputs.cache
C:\Users\Home\RiderProjects\ConsoleApplication1\ConsoleApplication1\obj\Debug\ConsoleApplication1.exe
C:\Users\Home\RiderProjects\ConsoleApplication1\ConsoleApplication1\obj\Debug\ConsoleApplication1.pdb
C:\Users\Niklas\RiderProjects\inf3_gitlab\Aufgabe3\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe
C:\Users\Niklas\RiderProjects\inf3_gitlab\Aufgabe3\ConsoleApplication1\bin\Debug\ConsoleApplication1.pdb
C:\Users\Niklas\RiderProjects\inf3_gitlab\Aufgabe3\ConsoleApplication1\obj\Debug\ConsoleApplication1.csprojAssemblyReference.cache
C:\Users\Niklas\RiderProjects\inf3_gitlab\Aufgabe3\ConsoleApplication1\obj\Debug\ConsoleApplication1.csproj.CoreCompileInputs.cache
C:\Users\Niklas\RiderProjects\inf3_gitlab\Aufgabe3\ConsoleApplication1\obj\Debug\ConsoleApplication1.exe
C:\Users\Niklas\RiderProjects\inf3_gitlab\Aufgabe3\ConsoleApplication1\obj\Debug\ConsoleApplication1.pdb
No preview for this file type
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment