Bakery Algorithm的c#实现用于多线程互斥访问临界资源
来源:互联网 发布:知乎 电脑下载 编辑:程序博客网 时间:2024/06/05 10:29
算法原理:每个线程进入的时候都要取得一个编号,对于不同线程编号较小的较先运行,编号相同时进程号小的先运行。
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(); } }}
0 0
- Bakery Algorithm的c#实现用于多线程互斥访问临界资源
- 用临界区实现互斥资源的访问
- php用apc实现的临界区 解决并发,资源互斥同步访问
- QT多线程临界资源互斥
- PV操作和信号量机制实现进程同步(对多个临界资源的互斥访问)
- 临界资源互斥的基本方法
- 关于Win32 API(VC作为程序设计语言)的创建线程,解决互斥访问临界资源问题
- java多线程资源互斥访问
- 线程同步的3种实现方法 互斥对象 临界资源 事件对象
- 进程同步的基本概念:临界资源、同步和互斥
- 多线程【临界区-临界资源的理解】
- 网络编程(38)—— 利用互斥mutex控制多线程对临界区的访问
- Redis实现lock互斥访问资源
- Java中多线程互斥访问的实现
- 多线程实现互斥访问对象的方法
- MSP430实现简单的对临界资源的冲突访问
- 临界资源 互斥访问 内核中的up和down函数
- C++ 临界区 多线程同步互斥
- spring中的bean何时被实例化
- VC++模拟鼠标点击
- Java中的Static关键字
- 链式队列定义以及应用
- 常用性能分析工具之sar、iostat
- Bakery Algorithm的c#实现用于多线程互斥访问临界资源
- Linux命令发送Http的get或post请求(curl和wget两种方法)
- 文件默认权限
- swift中UINavigationController的使用
- java 原码、反码、补码计算 以及 取反(~)运算
- 低电压检测
- P1309瑞士轮
- 14.4.1 Buffer Pool
- Android Studio错误: 非法字符: '\ufeff' 需要class, interface或enum