Bakery Algorithm的c#实现用于多线程互斥访问临界资源
2016-10-18 16:46
447 查看
算法原理:每个线程进入的时候都要取得一个编号,对于不同线程编号较小的较先运行,编号相同时进程号小的先运行。
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace Bakery { class Program { public static int[] data = new int[100000]; public static int total = 10; private static object ojb = new object(); static int num = 10; static int threads=4; static List<int> ticket = new List<int>(threads); // 保留票号 static List<bool> entering = new List<bool>(threads); //记录线程是否要进入 public static void addLock(int pid) // thread ID { //Console.WriteLine(pid); entering[pid] = true; int max = 0; for (int i = 0; i < threads; ++i) { int current = ticket[i]; if (current > max) { max = current; } } ticket[pid]=max+1; entering[pid]=false; for (int i = 0; i < ticket.Count; ++i) { if (i != pid) { while (entering[i] == true) { ; } // wait while other thread picks a ticket while (ticket[i] != 0 && ( ticket[pid] > ticket[i] || (ticket[pid] == ticket[i] && pid > i))) { ; } } } // The critical section goes here... } public static void unlock(int pid) { ticket[pid] = 0; } public static void changeValue(object pid) { int p = Convert.ToInt32(pid.ToString()); addLock(p); for (int i = 0; i < total; i++) { num = num + 1; Console.WriteLine(num); } unlock(p); } public static void readValue(object pid) { int p = Convert.ToInt32(pid.ToString()); addLock(p); for (int i = 0; i < total; i++) { num = num - 1; Console.WriteLine(num); } unlock(p); } static void Main(string[] args) { ticket = new List<int>(threads); entering = new List<bool>(threads); for (int i = 0; i < threads;i++ ) { ticket.Add(-1); entering.Add(false); } for (int i = 0; i < total; i++) { data[i] = 0; } Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); int num = 10; Thread[] t = new Thread[num]; t[0] = new Thread(new ParameterizedThreadStart(changeValue)); t[1] = new Thread(new ParameterizedThreadStart(readValue)); t[2] = new Thread(new ParameterizedThreadStart(readValue)); t[3] = new Thread(new ParameterizedThreadStart(changeValue)); stopwatch.Start(); t[0].Start(0); t[1].Start(1); t[2].Start(2); t[3].Start(3); t[0].Join(); t[1].Join(); t[2].Join(); t[3].Join(); stopwatch.Stop(); Console.WriteLine("运行时间" + stopwatch.ElapsedMilliseconds.ToString()); Console.ReadKey(); } } }
相关文章推荐
- 多线程实现互斥访问对象的方法
- 用WaitForSingleObject()函数实现简单的多线程互斥访问
- Java中多线程互斥访问的实现
- PV操作和信号量机制实现进程同步(对多个临界资源的互斥访问)
- 关于用于静态变量实现singleton的多线程互斥的一点疑问
- PV操作和信号量机制实现进程同步(对多个临界资源的互斥访问)
- Net/C#: 实现支持断点续传多线程下载的 Http Web 客户端工具类 (C# DIY HttpWebClient)
- Windows XP SP2如何实现多线程访问
- .Net/C#: 实现支持断点续传多线程下载的 Http Web 客户端工具类 (第2版) (C# DIY HttpWebClient)
- asp.Net(c#)中实现IP地址限制访问~
- 使文件下载的自定义连接支持 FlashGet 的断点续传多线程链接下载! C#/ASP.Net 实现
- .Net/C#: 实现支持断点续传多线程下载的 Http Web 客户端工具类 (第2版) (C# DIY HttpWebClient)
- C#中简单实现多线程
- .Net/C#: 实现支持断点续传多线程下载的 Http Web 客户端工具类 (C# DIY HttpWebClient)
- 用C#实现HTTP协议下的多线程文件传输
- 用C#实现蜘蛛/爬虫程序的多线程控制
- 用C#实现HTTP协议下的多线程文件传输--转载
- .Net/C#: 实现支持断点续传多线程下载的 Http Web 客户端工具类 (第2版) (C# DIY HttpWebClient)
- 用PV操作实现多线程间同步互斥(PV)
- /* .Net/C#: 实现支持断点续传多线程下载的 Http Web 客户端工具类【转】