المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Concurrency



C# Programming
04-09-2013, 10:03 PM
i am currently working on an example of concurrency using semaphores, and if it takes one semaphore and buffer 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 that the buffer just gets and puts the object and the sempahore creates the concurrency locks and releases the object (waits and Signals.. its just expanding on the buttonpanel to waitpanel to waitpanel1

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.******** = 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.******** = 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.******** = 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.******** = 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 form1public 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 Bufferpublic 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 Semaphorepublic 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 Buffer1public 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 Semaphore1public 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