The Dining Philosophers Problem. Part 3. TForks explanation.


This article continues the detailed program explanation. Now let's look at the ForksUnit.pas module that defines TForks class.

TForks class definition.

TForks class represents a fork manager. The main program creates one instance of the TForks. All philosophers (see the previous article for TPhilosopher class definition and explanation) use the same fork manager. TForks is derived from the TObject class and defines methods that allow philosophers to acquire forks and to put them back. The main program examines the "forks" property to determine and show the state of every fork.

TFork methods are thread-safe. Each philosopher runs the separate thread, so several synchronization and locking mechanisms should be used.

TFork class is defined like following:

type
    // Type for the array of mutexes
    THandleArray = array [WORD] of THandle;
    PHandleArray = ^THandleArray;
    // Type for the array of Boolean (each value represent the status of the fork)
    TBooleanArray = array [WORD] of boolean;
    PBooleanArray = ^TBooleanArray;

// The fork's manager
TForks = class
private
    nForks: integer; // number of forks
    aForks: PHandleArray; // array of mutexes
    // array of forks
    // if value is true - the fork is taken by the philosopher
    // false - the fork is on the table
    aForkStatus: PBooleanArray;
    mtxForkStatusLock : THandle; // mutex that guards aForkStatus array
    function GetForks(i: integer): boolean; // property get procedure for the Forks property
public
    destructor Destroy;override; // destructor - frees memory and destroys mutexes
    constructor Create(nForksNumber:integer); // allocates memory and creates mutexes
    function TakeFork(i:integer; Timeout: DWORD):Boolean; // processes the philosopher's fork request
    procedure ReturnFork(i:integer); // puts the fork back on the table
    procedure LockForkStatus; // locks aForkStatus array
    procedure UnLockForkStatus; // releases aForkStatus array
    property Count: integer read nForks; // number of the forks
    // if true - the fork is taken by the philosopher
    // false - the fork is on the table

    property Forks[i:integer]: boolean read GetForks; // status of each fork
end;

TForks private fields.

aForks is an array of the mutex objects (see Coordinating threads Part 3. Win32 objects. Mutex object  for the discussion of mutexes). A philosopher requests a fork by waiting on the corresponding mutex and returns a fork releasing the mutex.

There is also aForkStatus array that represents the status of each fork. The aForkStatus values are modified every time the philosopher takes or returns the fork. The aForkStatus array could be accessed from up to six threads (including the main thread) therefore it is guarded with the mtxForkStatusLock mutex.

TForks implementation.

Constructor TForks.Create takes one parameter - number of forks on the table and allocates memory for the array the mutexes (aForks)

The copyright of the article The Dining Philosophers Problem. Part 3. TForks explanation. in Delphi Programming is owned by Lyapin Ilya. Permission to republish The Dining Philosophers Problem. Part 3. TForks explanation. in print or online must be granted by the author in writing.

Go To Page: 1 2 3

Articles in this Topic    Discussions in this Topic