Ejemplo n.º 1
0
        public Philosopher(int ID, Fork lFork, Fork rFork, ref ListView infoLV)
        {
            philID    = ID;
            leftFork  = lFork;
            rightFork = rFork;

            pThread = new Thread(new ThreadStart(Dine));

            LV = infoLV;
        }
Ejemplo n.º 2
0
        private void interrupt(string single)
        {
            switch (single)
            {
            case "Start":
                lv_info.Items.Clear();
                btn_Start.Text = "Stop";

                numPhils     = (int)nud_numPhils.Value;
                forks        = new Fork[numPhils];
                philosophers = new Philosopher[numPhils];

                //create forks
                for (int i = 0; i < numPhils; i++)
                {
                    forks[i] = new Fork(i);
                }


                //create philosophers and start dining
                for (int i = 0; i < numPhils; i++)
                {
                    int rightforkID = i - 1;
                    if (i == 0)
                    {
                        rightforkID = numPhils - 1;
                    }

                    philosophers[i] = new Philosopher(i, forks[i], forks[rightforkID], ref lv_info);

                    ListViewItem lvi = new ListViewItem(i.ToString());
                    lvi.SubItems.Add("Waiting");
                    lvi.SubItems.Add("");
                    lvi.SubItems.Add("0");
                    lv_info.Items.Add(lvi);

                    philosophers[i].start((double)nud_Delay.Value);
                }

                break;

            case "Stop":

                btn_Start.Text = "Start";

                for (int i = 0; i < numPhils; i++)
                {
                    philosophers[i].stop(false);
                }

                break;
            }
        }