You'd probably want each section of tarmac to contain an array/list of tarmac sections that it's linked to. That way the plane's thread only has to look at a certain member of the current tarmac section to find the next section.Welshy said:Hopefully my final question, but what would be the best way to pass the Plane to the next section? As the array of Sections is a member in the MainWindow class, the Plane threads cannot access it directly. Passing the entire array into each plane seems a bit like overkill, maybe use a delegate to collect and return the next Section? Pretty much as I had before.
public void lockRunway(ref Plane iPlane)
{
lock (this)
{
while (mQueue)
{
Monitor.Wait(this);
}
while (mPlane != null)
{
mQueue = true;
//The runway section is in use
//by another plane, so block on
//this object.
Monitor.Wait(this);
}
//We have acquired the runway :-)!
mPlane = iPlane;
mQueue = false;
}
}
Why not?eriedor said:looks good except for, why are you passing iPlane as ref?
public class FCFS
{
private List<Thread> mQueue = new List<Thread>();
public FCFS()
{
}
public void enter()
{
Thread mThreadToQueue = Thread.CurrentThread;
try
{
lock (mQueue)
{
//queue the thread
mQueue.Add(mThreadToQueue);
}
lock (mThreadToQueue)
{
if (mQueue[0] != mThreadToQueue)
{
//take out its lock and block the thread on itself
Monitor.Wait(mThreadToQueue);
}
}
}
//If we reach here then we are the front of the queue - so we have acquired the lock :-)!
//if the thread is interrupted remove it from the list
catch (ThreadInterruptedException lExc)
{
mQueue.Remove(mThreadToQueue);
throw lExc;
}
}
public void exit()
{
mQueue.RemoveAt(0); //remove the thread just processed
if (mQueue.Count > 0)
{
//There is another thread waiting. . .
Thread lNext = mQueue[0];
lock (lNext)
{
//Wake it up
Monitor.Pulse(lNext);
}
}
}
}
Welshy said:Why not?
Well because you don't need to, I can only assume that you believe the ref keyword is used to pass the object reference and not a copy of the object? This is untrue, in C# classes are always handled by ref, types such as structs, int, bool etc are all passed by value.Welshy said:Why not?
Welshy said:Oh right, I'm more used to writing in C++ and needing to pass references
Lagz - the queue is as simple as it is, because no more than two planes can be queuing for the same section anyway, so no need
edit: for future reference, if i pass "this" into a method, is that also a reference? or have i passed by value?
Welshy said:Oh right, I'm more used to writing in C++ and needing to pass references
Lagz - the queue is as simple as it is, because no more than two planes can be queuing for the same section anyway, so no need
edit: for future reference, if i pass "this" into a method, is that also a reference? or have i passed by value?
using System;
using System.Collections.Generic;
using System.Text;
namespace PassObjectByRefOrValu
{
class FooBar
{
public string identity;
public FooBar(string identity) { this.identity = identity; }
}
class Program
{
static void Main(string[] args)
{
Program p = new Program();
p.Run();
}
private void Run()
{
FooBar foo = new FooBar("foo");
PassReferenceByValue(foo);
Console.WriteLine("foo.identity = '" + foo.identity + "' after call to PassReferenceByValue().");
// outputs "foo" as the foo object reference here still points to the object created above.
PassReferenceByReference(ref foo);
Console.WriteLine("foo.identity = '" + foo.identity + "' after call to PassReferenceByReference().");
// outputs "bar" as the foo object reference here now points to the new object created inside PassReferenceByReference() due to the ref keyword.
Console.ReadLine();
}
private void PassReferenceByValue(FooBar objrefByValue) {
objrefByValue = new FooBar("bar");
}
private void PassReferenceByReference(ref FooBar objrefByReference)
{
objrefByReference = new FooBar("bar");
}
}
}