Question for --> itsmeandnobodyelse: Continued help with shared mutex for multiple thread application


Taking my final comments from the last question which I closed, I'd like to apply them to the general idea you proposed below.  My threads are going to have this behavior:

1)  Obtain data by constantly reading sockets
2)  As the data is obtained they will access the Queue
3)  Once they have possesion to access the Queue they will seek out the head position and update it
4)  The main thread will make a decision as to whether the head of the queue has enough information to go on
5)  If all is well the head structure is removed/landed, otherwise it is moved to the end.

An alternative to the approach above is that each thread stores their own data structures and these data structures are globally accessed/shared so that information may be pulled into the queue by the primary thread/tower.

At any rate, I'd like to put something into my project today and see that something, anything, compiles which is related to a mutex library.  At this point, I don't have anything to make a header file with that supports mutex functions.  I need to see that I'm making that kind of headway  - thank you.

class LandingQueue
     Mutex                m_cs;       //  how is the variable type 'Mutex' supported ?  what # include <> ?

vector<Aircraft> m_aircrafts;
     void updateAircraftData(int aircraftID)
            // if we come here we have exclusive access

            Aircraft& cur = m_aircrafts[aircraftID];
            // make the update

            // Now the resource is unlocked
Who is Participating?
itsmeandnobodyelseConnect With a Mentor Commented:
>>>> 1)  Obtain data by constantly reading sockets

You are lucky. I am a specialist regarding sockets.

However, sockets mostly were used between processes. Between threads we can use notifications (messages) which were sent to each target's notification queue. That queue needs to be made thread-safe by a mutex. All threads need to evaluate their queues on a periodical base.

sockets come to play if you want to control the simulation in some kind, e. g. by manually announcing new airplanes or changing wind parameters ...

Then, your simulation threads need to act as a socket server which accepts clients and read external messages from these clients. You might use one client for all (means the client can emulate a new aircraft or a change of wind or others) for simplicity. However, for any (server) thread the client is independently external device which only sends specific messages.

>>>> (2)  As the data is obtained they will access the Queue

I changed the design so that an (aircraft) thread doesn't have access to the landing queue but only to the Aircraft object associated with the thread. The communication between an aircraft (thread) and the tower (thread) was supposed to work with notifications and updates of the Aircraft object. The tower has exclusive access to the LandingQueue and Runway information (no need to protect these objects fom mutual access).

>>>> 3)  Once they have possesion to access the Queue they will seek out the head position and update it

Actually I am week on how to run an airport tower. So, you would need to add the appropriate member data to any of the classes that is most suitable to hold that information. Generally, all decision work should be done by the Tower (maybe influenced by external messages via sockets) , while all information updates can be done by the objects themselves. E. g. an Aircraft will update its status informations periodically without asking the Tower for permission. But it will send a notification. The Tower will check its notifications and handle them. That might result to instructions to a single aircraft or the landing queue. You desrcibe my intentions with (4) and (5).

>>>> Good Answer

I saw, that you  accepted my last answer in the previous question. Thanks for the points.

I forgot to specify a Runway class, which would hold Landing and Wind information. Could you specify the attributes of such a class and add a member of that new class to LandingQueue (or add the attributes to the LandingQueue class if you like better).

>>>>  //  how is the variable type 'Mutex' supported ?  what # include <> ?

It's the class I posted in the last question. You might put the WIN32 part in a header, say mutex.h, and include it in your class headers. If you put it into your project source directory you would include it by

#include "mutex.h"

If you put it in a general include directory, it is

#include <mutex.h>

// mutex.h
#ifndef MUTEX_H
#define MUTEX_H

#include <windows.h>

class Mutex
    Mutex() {  InitializeCriticalSection(&mutex);  }

    // enter critical section and block others threads or wait
    void enter() { EnterCriticalSection(&mutex); }
    // leave critical section and release any other thread blocked
    void leave() { LeaveCriticalSection(&mutex); }


#endif // MUTEX_H

Regards, Alex

John500Author Commented:

I believe you have the answers I need to make this thing work and for that reason, I want to shoot you another 500 points and open yet another question.

I'm hoping this will inspire you/cause you to consider exactly what I've done so far so that you can apply your solutions to my current state.

In other words, I almost need you to drop any ideas you currently have and wholey accept what I'm telling you about the simplicity of what I'm doing.  I don't work for an airport and I'd rather not get into a lot of unnecessary detail.

But brieflyy, I was tasked first and for-most to prove that I could read three socket transmissions off the network.  They are:

1)  Aircraft info to include final approach, but limitted to three elements (pilot, line-up position, sidenumber)
2)  Radar info which only includes (sidenumber, distance)
3)  Wind information at the runway

Using a .Net Form Application I proved I could capture all three types of information and displpay them simultaneously to the Form or Panel as I call it.

Now that I have captured this information, I've been asked to use the information to coordinate a landing based on the conditions I've already mentioned.  The total elements as you can see are close to _nothing_, _nothing_ .  I just want to come up with a simple design that will take my existing thread information and use it to allow or disallow the aircraft identified on final to land or not land.  Two simple checks !   Side number and wind !

My gut feeling tells me that I can do this in probably five minutes if I knew how.  I only want to create a system for ordering --> a queue.   Then I want to make the structures in that queue move in a circular fashion --> an array.

How quickly can this be done ?  How much code do I need to take existing information from three threads and share it ?

I have the Queue code but I don't have anything that supports the mutex part.  I imagine that the queue structures will be included in the mutex class but I can't see it as yet :

typedef struct {
                                                        //  aircraft info
     string               LineUpNumber;     // on final or not             #1
     string               ACSideNum;         // aircraft side number    #2              
                                                      // Wind data
     float               RelWndDir;           // relative wind direction  #3
     float               RelWndSpd;         //  speed                          #4
     float               TrueWndDir;         //  true wind direction      #5
     float               TrueWndSpd;       //  speed
                                                        // radar info
     string               RadarSideNum;     //  aircraft side number    #6
     float               RadarRange;           //  distance                      #7

typedef struct {                                              
     LandingInfo elements[MAX_Q_SIZE];      // static queue
     int head;                                              //  head
     int tail;                                                 //  tail

I can't remember how to use the head & tail with an arrary so that needs done also.  Where do we start ?  But remember as __simple__ as absolutely possible.

John500Author Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.