Esempio n. 1
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;
            }
        }
Esempio n. 2
0
        public void RequestFork(Philosopher phil)
        {
            if (holder == null || ((holder.philID) % 4 > phil.philID && holder.eating == false))
            {
                holder = phil;
            }
            else
            {
                while (holder != null)
                {
                    Thread.Sleep(0);
                }

                holder = phil;
            }
        }
Esempio n. 3
0
 public void CleanFork()
 {
     holder = null;
 }