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 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
    {
        string logDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "DiningPhilosophersLogs");
        if (Directory.Exists(logDir))
        {
            foreach (string file in Directory.GetFiles(logDir))
            {
                File.Delete(file);
            }
        }

        LogManager.LogAdapter = new NLogLogAdapter(logDir);

        int philos, total, amount, maxsleep;

        GetArgsOrDefaults(args, out philos, out total, out amount, out maxsleep);

        Mutex[] chopSticks = new Mutex[philos];

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

        Philosopher[] philosophers = new Philosopher[philos];
        // Create the Philosophers
        for (int i = 0; i < philos; i++)
        {
            PhilosopherData pd;
            pd.PhilosopherId = i + 1;
            pd.RightChopStick = chopSticks[(i - 1 + philos) % philos];
            pd.LeftChopStick = chopSticks[i];
            pd.AmountToEat = amount;
            pd.TotalFood = total;
            pd.MaxSleep = 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 . . .", philos);

        for (int i = 0; i < philos; 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();
    }
}

private static void GetArgsOrDefaults(string[] args, out int philos, out int total, out int amount, out int maxsleep)
{
    philos = DefaultPhilos;
    total = DefaultTotalToEat;
    amount = DefaultAmountToEat;
    maxsleep = DefaultMaxSleep;
    if (args.Length == 0)
        return;
    if (args.Length == 4)
    {
        Int32.TryParse(args[0], out philos);
        Int32.TryParse(args[1], out total);
        Int32.TryParse(args[2], out amount);
        Int32.TryParse(args[3], out maxsleep);
        return;
    }
    Console.WriteLine("usage: DiningPhilosophers [philos totalToEat amountToEat maxSleep] - using defaults");
}