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 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.
Articles in this Topic
Discussions in this Topic
|