Concurrency
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Concurrency

  1. #1
    Join Date
    Apr 2013
    Posts
    4

    Unhappy Concurrency

    i am currently working on an example of concurrency using semaphores, and if it takes one semaphore and buffer (see UML) in this example to pass the object from buttonpanel to waitpanel. then you would need another semaphore and buffer to pass from waitpanel to waitpanel1, what i'm having problems with is how this is reflected in the code, the relationship and initializing the semaphore to semaphore1 and buffer to buffer1.. i understand the uml but doing this with the code is another matter..




    Code:
    using System;
    
    using System.Windows.Forms;
    using System.Threading;
    using System.ComponentModel;
    using System.Collections;
    using System.Data;
    using System.Drawing;
    
    public class Form1 : Form
    {
        private Container components = null;
        private ButtonPanelThread p1;
        private Button btn1;
        private WaitPanelThread p2;
        private WaitPanelThread1 p3;
        private Thread thread1, thread2, thread3;
        private Semaphore semaphore;
        private Semaphore1 semaphore1;
        private Buffer buffer;
        private Buffer1 buffer1;
        private Thread semThread,semThread1;
        private Thread buffThread,buffThread1;
        private Panel pnl1, pnl2, pnl3;
    
        public Form1()
        {
            InitializeComponent();
    
            semaphore = new Semaphore();
            semaphore1 = new Semaphore1();
            buffer = new Buffer();
            buffer1 = new Buffer1();
    
            p1 = new ButtonPanelThread(new Point(40, 10), 120, true, pnl1, Color.Blue, semaphore, buffer, btn1);
    
            p2 = new WaitPanelThread(new Point(5, 10), 200, true, pnl2, Color.White, semaphore, buffer);
            p3 = new WaitPanelThread1(new Point(5, 10), 200, true, pnl3, Color.White, semaphore1, buffer1);
    
            semThread = new Thread(new ThreadStart(semaphore.Start));
            semThread1 = new Thread(new ThreadStart(semaphore1.Start));
    
            buffThread = new Thread(new ThreadStart(buffer.Start));
            buffThread1 = new Thread(new ThreadStart(buffer1.Start));
    
            thread1 = new Thread(new ThreadStart(p1.Start));
            thread2 = new Thread(new ThreadStart(p2.Start));
            thread3 = new Thread(new ThreadStart(p3.Start));
    
            this.Closing += new CancelEventHandler(this.Form1_Closing);
    
            semThread.Start();
            buffThread.Start();
    
            thread1.Start();
            thread2.Start();
            thread3.Start();
        }
    
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                    components.Dispose();
            }
            base.Dispose(disposing);
        }
    
        private void InitializeComponent()
        {
            this.pnl1 = new System.Windows.Forms.Panel();
            this.btn1 = new System.Windows.Forms.Button();
            this.pnl2 = new System.Windows.Forms.Panel();
            this.pnl3 = new System.Windows.Forms.Panel();
            this.pnl1.SuspendLayout();
            this.SuspendLayout();
            // 
            // pnl1
            // 
            this.pnl1.BackColor = System.Drawing.Color.White;
            this.pnl1.Controls.Add(this.btn1);
            this.pnl1.Location = new System.Drawing.Point(29, 200);
            this.pnl1.Name = "pnl1";
            this.pnl1.Size = new System.Drawing.Size(260, 30);
            this.pnl1.TabIndex = 0;
            // 
            // btn1
            // 
            this.btn1.BackColor = System.Drawing.Color.Pink;
            this.btn1.Location = new System.Drawing.Point(0, 0);
            this.btn1.Name = "btn1";
            this.btn1.Size = new System.Drawing.Size(30, 30);
            this.btn1.TabIndex = 0;
            this.btn1.UseVisualStyleBackColor = false;
            // 
            // pnl2
            // 
            this.pnl2.BackColor = System.Drawing.Color.White;
            this.pnl2.Location = new System.Drawing.Point(295, 200);
            this.pnl2.Name = "pnl2";
            this.pnl2.Size = new System.Drawing.Size(260, 30);
            this.pnl2.TabIndex = 1;
            // 
            // pnl3
            // 
            this.pnl3.BackColor = System.Drawing.Color.White;
            this.pnl3.Location = new System.Drawing.Point(561, 200);
            this.pnl3.Name = "pnl3";
            this.pnl3.Size = new System.Drawing.Size(260, 30);
            this.pnl3.TabIndex = 2;
            // 
            // Form1
            // 
            this.BackColor = System.Drawing.Color.LightGray;
            this.ClientSize = new System.Drawing.Size(861, 462);
            this.Controls.Add(this.pnl1);
            this.Controls.Add(this.pnl2);
            this.Controls.Add(this.pnl3);
            this.Name = "Form";
            this.Text = "Test";
            this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
            this.pnl1.ResumeLayout(false);
            this.ResumeLayout(false);
    
        }
    
        private void Form1_Closing(object sender, CancelEventArgs e)
        {
            // Environment is a System class.
            // Kill off all threads on exit.
            Environment.Exit(Environment.ExitCode);
        }
    
    }// end class form1
    
    public class Buffer
    {
        private Color objectColor;
        private bool empty = true;
    
        public void Read(ref Color objectColor)
        {
            lock (this)
            {
                // Check whether the buffer is empty.
                if (empty)
                    Monitor.Wait(this);
                empty = true;
                objectColor = this.objectColor;
                Monitor.Pulse(this);
            }
        }
    
        public void Write(Color objectColor)
        {
            lock (this)
            {
                // Check whether the buffer is full.
                if (!empty)
                    Monitor.Wait(this);
                empty = false;
                this.objectColor = objectColor;
                Monitor.Pulse(this);
            }
        }
    
        public void Start()
        {
        }
    
    }// end class Buffer
    
    public class Semaphore
    {
        private int count = 0;
    
        public void Wait()
        {
            lock (this)
            {
                while (count == 0)
                    Monitor.Wait(this);
                count = 0;
            }
        }
    
        public void Signal()
        {
            lock (this)
            {
                count = 1;
                Monitor.Pulse(this);
            }
        }
    
        public void Start()
        {
        }
    
    }// end class Semaphore
    
    public class Buffer1
    {
        private Color objectColor;
        private bool empty = true;
    
        public void Read(ref Color objectColor)
        {
            lock (this)
            {
                // Check whether the buffer is empty.
                if (empty)
                    Monitor.Wait(this);
                empty = true;
                objectColor = this.objectColor;
                Monitor.Pulse(this);
            }
        }
    
        public void Write(Color objectColor)
        {
            lock (this)
            {
                // Check whether the buffer is full.
                if (!empty)
                    Monitor.Wait(this);
                empty = false;
                this.objectColor = objectColor;
                Monitor.Pulse(this);
            }
        }
    
        public void Start()
        {
        }
    
    }// end class Buffer1
    
    public class Semaphore1
    {
        private int count = 0;
    
        public void Wait()
        {
            lock (this)
            {
                while (count == 0)
                    Monitor.Wait(this);
                count = 0;
            }
        }
    
        public void Signal()
        {
            lock (this)
            {
                count = 1;
                Monitor.Pulse(this);
            }
        }
    
        public void Start()
        {
        }
    
    }// end class Semaphore1
    
    public class ButtonPanelThread
    {
        private Point origin;
        private int delay;
        private Panel panel;
        private bool westEast;
        private Color colour;
        private Point obj;
        private int xDelta;
        private int yDelta;
        private Semaphore semaphore;
        private Buffer buffer;
        private Button btn;
        private bool locked = true;
    
        public ButtonPanelThread(Point origin, int delay, bool westEast, Panel panel,Color colour, Semaphore semaphore, Buffer buffer, Button btn)
        {
            this.origin = origin;
            this.delay = delay;
            this.westEast = westEast;
            this.panel = panel;
            this.colour = colour;
            this.obj = origin;
            this.panel.Paint += new PaintEventHandler(this.panel_Paint);
            this.xDelta = westEast ? +10 : -10;
            this.yDelta = 0;
            this.semaphore = semaphore;
            this.buffer = buffer;
            this.btn = btn;
            this.btn.Click += new System.EventHandler(this.btn_Click);
        }
    
        private void btn_Click(object sender, System.EventArgs e)
        {
            locked = !locked;
            this.btn.BackColor = locked ? Color.Pink : Color.LightGreen;
            lock (this)
            {
                if (!locked)
                    Monitor.Pulse(this);
            }
        }
    
        public void Start()
        {
            Color signal = Color.Red;
            Thread.Sleep(delay);
    
            for (int k = 1; k <= 1; k++)
            {
                this.zeroObject();
                panel.Invalidate();
                lock (this)
                {
                    while (locked)
                    {
                        Monitor.Wait(this);
                    }
                }
                for (int i = 1; i <= 20; i++)
                {
                    this.moveObject(xDelta, yDelta);
                    Thread.Sleep(delay);
                    panel.Invalidate();
                }
                semaphore.Wait();
                buffer.Write(this.colour);
            }
            this.colour = Color.Gray;
            panel.Invalidate();
        }
    
        private void zeroObject()
        {
            obj.X = origin.X;
            obj.Y = origin.Y;
        }
    
        private void moveObject(int xDelta, int yDelta)
        {
            obj.X += xDelta; obj.Y += yDelta;
        }
    
        private void panel_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
    
            SolidBrush brush = new SolidBrush(colour);
            g.FillRectangle(brush, obj.X, obj.Y, 10, 10);
    
            brush.Dispose();    //  Dispose graphics resources. 
            g.Dispose();        //  
        }
    }// end class ButtonPanelThread
    
    public class WaitPanelThread
    {
        private Point origin;
        private int delay;
        private Panel panel;
        private bool westEast;
        private Color colour;
        private Point obj;
        private int xDelta;
        private int yDelta;
        private Semaphore semaphore;
        private Buffer buffer;
    
        public WaitPanelThread(Point origin, int delay, bool westEast, Panel panel, Color colour, Semaphore semaphore, Buffer buffer)
        {
            this.origin = origin;
            this.delay = delay;
            this.westEast = westEast;
            this.panel = panel;
            this.colour = colour;
            this.obj = origin;
            this.panel.Paint += new PaintEventHandler(this.panel_Paint);
            this.xDelta = westEast ? +10 : -10;
            this.yDelta = 0;
            this.semaphore = semaphore;
            this.buffer = buffer;
        }
    
        public void Start()
        {
            //Thread.Sleep(delay);
            this.colour = Color.White;
            for (int k = 1; k <= 10; k++)
            {
                semaphore.Signal();
                this.zeroObject();
    
                buffer.Read(ref this.colour);
    
                for (int i = 1; i <= 23; i++)
                {
                    panel.Invalidate();
                    this.moveObject(xDelta, yDelta);
                    Thread.Sleep(delay);
                }
                this.colour = Color.White;
                panel.Invalidate();
            }
            this.colour = Color.Gray;
            panel.Invalidate();
        }
    
        private void zeroObject()
        {
            obj.X = origin.X;
            obj.Y = origin.Y;
        }
    
        private void moveObject(int xDelta, int yDelta)
        {
            obj.X += xDelta; obj.Y += yDelta;
        }
    
        private void panel_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            SolidBrush brush = new SolidBrush(colour);
            g.FillRectangle(brush, obj.X, obj.Y, 10, 10);
            brush.Dispose();    //  Dispose graphics resources. 
            g.Dispose();        //  
        }
    }// end class WaitPanelThread
    
    public class TheOne
    {
        public static void Main()//
        {
            Application.Run(new Form1());
        }
    }// end class TheOne
    
    public class WaitPanelThread1
    {
        private Point origin;
        private int delay;
        private Panel panel;
        private bool westEast;
        private Color colour;
        private Point obj;
        private int xDelta;
        private int yDelta;
        private Semaphore1 semaphore1;
        private Buffer1 buffer1;
    
        public WaitPanelThread1(Point origin, int delay, bool westEast, Panel panel, Color colour, Semaphore1 semaphore1, Buffer1 buffer1)
        {
            this.origin = origin;
            this.delay = delay;
            this.westEast = westEast;
            this.panel = panel;
            this.colour = colour;
            this.obj = origin;
            this.panel.Paint += new PaintEventHandler(this.panel_Paint);
            this.xDelta = westEast ? +10 : -10;
            this.yDelta = 0;
            this.semaphore1 = semaphore1;
            this.buffer1 = buffer1;
        }
    
        public void Start()
        {
            //Thread.Sleep(delay);
            this.colour = Color.White;
            for (int k = 1; k <= 10; k++)
            {
                semaphore1.Signal();
                this.zeroObject();
    
                buffer1.Read(ref this.colour);
    
                for (int i = 1; i <= 23; i++)
                {
                    panel.Invalidate();
                    this.moveObject(xDelta, yDelta);
                    Thread.Sleep(delay);
                }
                this.colour = Color.White;
                panel.Invalidate();
            }
            this.colour = Color.Gray;
            panel.Invalidate();
        }
    
        private void zeroObject()
        {
            obj.X = origin.X;
            obj.Y = origin.Y;
        }
    
        private void moveObject(int xDelta, int yDelta)
        {
            obj.X += xDelta; obj.Y += yDelta;
        }
    
        private void panel_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            SolidBrush brush = new SolidBrush(colour);
            g.FillRectangle(brush, obj.X, obj.Y, 10, 10);
            brush.Dispose();    //  Dispose graphics resources. 
            g.Dispose();        //  
        }
    }// end class WaitPanelThread
    Attached Images Attached Images  

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    10,956

    Re: Concurrency

    I believe I can help you, but to make it easier for me to do so, can you zip up the solution and post it here (no rar files please)?

    I see some locking issues, but I want to make sure I am correct before replying.

  3. #3
    Join Date
    Apr 2013
    Posts
    4

    Re: Concurrency

    Thank you for your response this is just a test program as the the main project will have 3 button panels and 6 waitpanels but the process should be the same.. i'm trying to break it down into small steps
    Attached Files Attached Files

  4. #4
    Join Date
    Apr 2013
    Posts
    4

    Re: Concurrency

    its the buttonpanel to waitpanel to waitpanel1 that has me banging my head against the wall

  5. #5
    Join Date
    Apr 2013
    Posts
    4

    Re: Concurrency

    have you had chance to review the zip file Arjay

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center