LAUREL BRIDGE

LaurelBridge.DCF.Examples.DiningPhilosophers Namespace

DICOM Connectivity Framework V3.4
The DiningPhilosophers example demonstrates how to configure DCF to perform session based logging using an NLog adapter.
Classes

  ClassDescription
Public classOptions
Command line options class for parsing user options for DiningPhilosophers.
Public classPhilosopher
A Philosopher class that demonstrates per-session logging.
Public classProgram
This example demonstrates DCF session logging by running a number of threads that each log to their own session based logfile.
Public classWorkerThread
WorkerThread is an abstract base class extended by the Philosopher class to implement thread behavior.
Structures

  StructureDescription
Public structurePhilosopherData
The data items that every dining philosopher needs.
Examples

DiningPhilosophers Sample Code
public static void Main(string[] args)
{
    try
    {
        Options options;
        if (!Options.TryParse(args, out options))
            return;
        string logDir = string.IsNullOrEmpty(options.LogDirectory) ? options.LogDirectory = DefaultLogOutputDirectory : options.LogDirectory;
        if (Directory.Exists(logDir))
        {
            foreach (string file in Directory.GetFiles(logDir))
            {
                File.Delete(file);
            }
        }

        LogManager.LogAdapter = new NLogLogAdapter(logDir);

        Mutex[] chopSticks = new Mutex[options.PhilosophersCount];

        // init the chopSticks
        for (int i = 0; i < options.PhilosophersCount; i++)
            chopSticks[i] = new Mutex(false);

        Philosopher[] philosophers = new Philosopher[options.PhilosophersCount];
        // Create the Philosophers
        for (int i = 0; i < options.PhilosophersCount; i++)
        {
            PhilosopherData pd;
            pd.PhilosopherId = i + 1;
            pd.RightChopStick = chopSticks[(i - 1 + options.PhilosophersCount) % options.PhilosophersCount];
            pd.LeftChopStick = chopSticks[i];
            pd.AmountToEat = options.AmountToEat;
            pd.TotalFood = options.TotalFood;
            pd.MaxSleep = options.MaxSleep;
            philosophers[i] = new Philosopher(pd);
            philosophers[i].Start();
        }
        string logFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "DCFLogs");
        Console.WriteLine("Output logs from this example are written to: {0}", logFolder);
        Console.WriteLine("Waiting for the {0} philosophers threads to finish . . .", options.PhilosophersCount);

        for (int i = 0; i < options.PhilosophersCount; i++)
        {
            philosophers[i].Join();
        }
    }
    catch (Exception e)
    {
        Console.WriteLine("Exception caught during execution: {0}", e);
        Logger.Error(e, "Exception caught during execution");
    }

    if (Debugger.IsAttached)
    {
        Console.Write("Press any key to continue . . . ");
        Console.ReadKey();
    }
}