Thread Synchronization (C# Programming Guide) (2010-02-05)
นำไปประยุกต์ใช้กับ multithreaded applications ในการเข้าถึง resources ที่ใช้ร่วมกันแบบง่ายๆ ก่อนนะครับ
อันแรกใช้ Lock Statement ครับ
For example:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading;
namespace SyncronizeThread { class Program { public static Int32 counter = new Int32(); static System.Object myLock = new object(); private static Mutex mut = new Mutex();
static void Main(string[] args) {
counter = 1; Thread t = new Thread(Run); t.Name = "a"; t.Start(); t = new Thread(Run); t.Name = "b"; t.Start(); t = new Thread(Run); t.Name = "c"; t.Start(); Console.ReadKey(); }
public static void Run() { Console.WriteLine(Thread.CurrentThread.Name + " Starting "); lock (myLock) { for (int i = 0; i < 10; i++) { Console.WriteLine(Thread.CurrentThread.Name + "===>" + (counter++)); Thread.Sleep(500); } }
} } } อีกอันนึงใช้ mutex using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading;
namespace SyncronizeThread { class Program { public static Int32 counter = new Int32(); static System.Object myLock = new object(); private static Mutex mut = new Mutex();
static void Main(string[] args) {
counter = 1; Thread t = new Thread(Run); t.Name = "a"; t.Start(); t = new Thread(Run); t.Name = "b"; t.Start(); t = new Thread(Run); t.Name = "c"; t.Start(); Console.ReadKey(); }
public static void Run() { Console.WriteLine(Thread.CurrentThread.Name + " Starting "); mut.WaitOne(); for (int i = 0; i < 10; i++ ) { Console.WriteLine(Thread.CurrentThread.Name + "===>" + (counter++)); Thread.Sleep(500); } mut.ReleaseMutex();
} } } อันนี้ใช้ Semaphore using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading;
namespace SyncronizeThread { class Program { public static Int32 counter = new Int32(); static System.Object myLock = new object(); private static Mutex mut = new Mutex(); static Semaphore s = new Semaphore(2, 2);
static void Main(string[] args) {
counter = 1; Thread t = new Thread(Run); t.Name = "a"; t.Start(); t = new Thread(Run); t.Name = "b"; t.Start(); t = new Thread(Run); t.Name = "c"; t.Start(); Console.ReadKey(); }
public static void Run() { Console.WriteLine(Thread.CurrentThread.Name + " Starting "); s.WaitOne(); for (int i = 0; i < 10; i++ ) { Console.WriteLine(Thread.CurrentThread.Name + "===>" + (counter++)); Thread.Sleep(500); } s.Release();
} } }
ผลการรันด้วย Lock Statement
a Starting
a===>1
b Starting
c Starting
a===>2
a===>3
a===>4
a===>5
a===>6
a===>7
a===>8
a===>9
a===>10
c===>11
c===>12
c===>13
c===>14
c===>15
c===>16
c===>17
c===>18
c===>19
c===>20
b===>21
b===>22
b===>23
b===>24
b===>25
b===>26
b===>27
b===>28
b===>29
b===>30
ผลการรันด้วย Mutex
a Starting
a===>1
b Starting
c Starting
a===>2
a===>3
a===>4
a===>5
a===>6
a===>7
a===>8
a===>9
a===>10
c===>11
c===>12
c===>13
c===>14
c===>15
c===>16
c===>17
c===>18
c===>19
c===>20
b===>21
b===>22
b===>23
b===>24
b===>25
b===>26
b===>27
b===>28
b===>29
b===>30
ผลการรันด้วย Semaphore
a Starting a===>1 c Starting c===>2 b Starting a===>3 c===>4 a===>5 c===>6 a===>7 c===>8 a===>9 c===>10 a===>11 c===>12 a===>13 c===>14 a===>15 c===>16 a===>17 c===>18 a===>19 c===>20 b===>21 b===>22 b===>23 b===>24 b===>25 b===>26 b===>27 b===>28 b===>29 b===>30
จะเห็นว่าสามารถเข้าใช้งานตัวแปร counter (resouce)ได้พร้อมกัน สอง thread
จะเห็นว่า Lock กับ Mutext thread จะใช้ resource ได้ทีละ thread, ต่างกันตรง Mutex มี scope การทำงานข้าม Process ได้
โดยระบุชื่อของ mutex เช่น Mutex mut = new Mutex(false, "mutexName");
แต่ Lock จำกัดอยู่ใน Process เดียวเท่านั้น ส่วน Semaphore ระบุจำนวน thread ที่จะเข้ามาใช้ resource ได้ครับ สุดยอดไปเรยยยยยยย
บทความที่น่าสนใจ www.c-sharpcorner.com/UploadFile/mmehta/Multithreading311162005045743AM/Multithreading3.aspx
Thread State Diagram
|