C# Multithreading Synchronisation

Soldato
Joined
9 Jul 2006
Posts
3,322
Location
London
Hey guys. I wouldnt usually post asking for help with assignments on here, but I feel like im going round in circles with this one.
Basically clicking on each "Hanger" spawns an aeroplane, current represented by coloured squares, they then follow the runway round to the takeoff section. The assignment spec is very vague, but you get the idea.

I'm having problems with race conditions that appear when two planes are queueing for the same section of tarmac. They keep running out of sync and overwriting eachothers data. I'm hoping for a few tips/pointers, maybe a good way of queueing them so that one isnt left waiting indefinetly?


The VS 2005 project file, source code, and assignment spec can be found here.

Thanks in advance :)
 
Yeah I was thinking about making some sort of queue list, ill give it a go since I've had no other ideas, cheers mate :)
 
Inquisitor said:
What you really need is a way of signalling between threads, for which wait handles can be very useful (specifically AutoResetEvent and ManualResetEvent; read up on them here).

You could just have a queue of wait handles for each section of tarmac that represent the planes waiting for it. When the plane currently on it finishes, just signal the first waiting thread by setting the first wait handle in the queue.

Of course, that's assuming you're assigning a thread to handle each plane permanently (I haven't looked very thoroughly through your code :p).
No, i've got each section of tarmac as a seperate thread, and they just pass the plane details between eachother. I'll have a look at waits aswell, nice one lads :D

Just changed the background images, so now instead of buttons and a plain coloured form, i have photos of hangers and a grass field :p
 
Giving up for the night now, been messing around with it for a good 6 hours now :eek:

Will post back tomorrow with yet more questions/problems :D
 
I did think about having one thread per plane, but I couldn't think of a good way to do it. If somebody could outline how to have each plane as a thread then it would be appreciated.

Edit: Bear in mind this is the first time I've EVER used threads :p
 
Last edited:
Lagz said:
You can implement ordering by adding waiting threads to a list, and then only waking up the plane at the head of the list.
What are these wait threads? I've read about them, but they're used in a context that I can't adapt to this assignment. Are they like a ThreadPool? :confused:
 
Ok right, I'm getting somewhere now.

I have the MainWindow class creating a new thread when you click on a hanger, which starts the "run()" method in the Plane class.

I have an Array of Sections and an ArrayList of Planes in the MainWindow class.

The planes are created and threads started no problem at all, and the planes move to the bottom of the first sections of tarmac :)

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.
 
Woo I've got them going round happily, and queuing as they should :D

Using a slightly modified version of the method Lagz provided. Added a very basic queue mechanism to it, which is no doubt going to get ripped apart as soon as I post this :p

Code:
        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;
            }
        }

Can't thank you guys enough for the help :)
 
Oh right, I'm more used to writing in C++ and needing to pass references :p

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?
 
Back
Top Bottom