Tag: 并发

潜在的并发问题?

我一直在构建ASP.NET MVC应用程序,当我启动它时,我担心潜在的multithreading问题。 一个特别关注的是以下代码: private static IDictionary _settingsDictionary = new Dictionary(); public T Settings() where T : ISettings, new() { var key = typeof(T).FullName; if (!_settingsDictionary.ContainsKey(key)) _settingsDictionary[key] = _settingsService.GetSettings(); return (T)_settingsDictionary[key]; } 请注意,字典定义为静态。 这允许我缓存字典,以便它为应用程序长度的每个请求返回相同的实例。 这在本地测试时工作正常,但我担心它可能会被数百名用户使用。 这使我开始研究ConcurrencyDictionary。 请问你能告诉我是否需要使用它以及如果是这样我会怎么做。 谢谢

使用ThreadPool.QueueUserWorkItem对线程进行排序

我是线程基础知识的新手。 我有一个操作队列要对XML文件执行(节点添加,节点删除等) 1]有’n’xml文件,并且对于每个文件,使用ThreadPool.QueueUserWorkItem分配来自线程池的线程来执行这些文件操作。 我想使用线程实现并发和操作顺序(重要)。 例如:假设要对文件“A.xml”执行操作[a1,a2,a3,a4,a5] 和操作[b1,b2,b3,b4,b5,b6,b7]将在文件“B.xml”上执行….. 我想分配线程,以便我可以执行这些操作 相同的顺序,也同时(因为文件不同)。 2]也可以为每个操作分配一个线程并实现可靠性并保持顺序。 在STA模型中我做了类似的事情.. while(queue.count>0){ File f = queue.Dequeue(); //get File from queue OperationList oprlst = getOperationsForFile(f); // will get list-> [a1,a2,a3,a4,a5] for each Operation oprn in oprlst { performOperation(f,oprn) //in MTA i want to wait till operation “a1” completes and then operation “a2” will //start.making threads wait till […]

实现无锁队列(对于Logger组件)

我正在设计一个新的改进的Logger组件(.NET 3.5,C#)。 我想使用无锁实现。 记录事件将从(可能)多个线程发送,尽管只有一个线程将实际输出到文件/其他存储介质。 从本质上讲,所有编写器都将它们的数据排入某个队列,然后由其他一些进程(LogFileWriter)进行检索。 这可以通过无锁方式实现吗? 我无法在网上找到这个特定问题的直接参考。

明确地实现IAsyncResult

我一般都对部分实现接口持谨慎态度。 但是, IAsyncResult有点特殊情况,因为它支持几种截然不同的使用模式。 使用/看到使用AsyncState / AsyncCallback模式的AsyncState是AsyncState ,而不是只使用AsyncWaitHandle调用EndInvoke ,或者轮询IsCompleted ( AsyncWaitHandle )? 相关问题: 检测ThreadPool WorkItem是否已完成/等待完成 。 考虑这个类(非常近似,需要锁定): public class Concurrent { private ManualResetEvent _resetEvent; private T _result; public Concurrent(Func f) { ThreadPool.QueueUserWorkItem(_ => { _result = f(); IsCompleted = true; if (_resetEvent != null) _resetEvent.Set(); }); } public WaitHandle WaitHandle { get { if (_resetEvent == […]

基于参数锁定

假设我有这个方法: void Foo(int bar) { // do stuff } 这是我想让Foo拥有的行为: 如果线程1调用Foo(1)而线程2调用Foo(2) ,则两个线程可以并发运行。 如果线程1调用Foo(1)而线程2调用Foo(1) ,则两个线程不能同时运行。 在.net有一个很好的标准方法来指定这种行为吗? 我有一个解决方案,使用对象字典来锁定,但这感觉有点混乱。

.NET ConcurrentDictionary初始容量设置为MSDN示例文档中的任意素数而不是预期容量。 为什么?

我只是在查看ConcurrentDictionary的MSDN文档 ,我在“示例”代码中看到了这一点: // We know how many items we want to insert into the ConcurrentDictionary. // So set the initial capacity to some prime number above that, to ensure that // the ConcurrentDictionary does not need to be resized while initializing it. int NUMITEMS = 64; int initialCapacity = 101; 作为参考,MSDN示例中的字典初始化如下: ConcurrentDictionary cd = new […]

写一个锁是否保证在另一个线程中读取新内容? (.Net,内存模型)

假设我有一个属性,其setter受锁保护,但没有任何锁定在getter周围,例如 private long _myField; public long MyProperty { get { return _myField; } set { lock(whatever) _myField = value; } } 除了同步写入(但不是读取)之外,锁定或者说Monitor.Exit应该导致易失性写入 。 我们现在说我们有两个线程A和B,并且发生以下序列: A读取MyProperty的当前值。 B将新值写入MyProperty 。 A再次读取MyProperty的当前值。 问:A现在可以保证看到新值吗? 或者我们的锁只是确保B及时写入主内存,而不是其他线程读取新值? 或者答案可能取决于我们是在运行.Net 2+还是“弱”的ECMA实施?

为什么GetThreadTimes会返回

我正在尝试测量线程在进度报告中花费的时间,但是我从GetThreadTimes系统调用得到了非常奇怪的结果。 鉴于以下程序(在VS 2013中编译,以.NET 4.5为目标): using System; using System.Diagnostics; using System.Runtime.InteropServices; using System.Threading; namespace ThreadTimingTest { class Program { static Stopwatch _wallClockTimer; static System.Timers.Timer _timer = new System.Timers.Timer(); private static Thread _thread; private static IntPtr _threadHandle; static void Main(string[] args) { _timer = new System.Timers.Timer(); _timer.Elapsed += (s, e) => { System.Runtime.InteropServices.ComTypes.FILETIME start, end, rawKernelTime, rawUserTime; […]

Linq缓存数据值 – 主要的并发问题?

这是我做的一个小实验: MyClass obj = dataContext.GetTable().Where(x => x.ID = 1).Single(); Console.WriteLine(obj.MyProperty); // output = “initial” Console.WriteLine(“Waiting…”); // put a breakpoint after this line obj = null; obj = dataContext.GetTable().Where(x => x.ID = 1).Single(); // same as before, but reloaded Console.WriteLine(obj.MyProperty); // output still = “initial” obj.MyOtherProperty = “foo”; dataContext.SubmitChanges(); // throws concurrency exception 当我在第3行之后点击断点时,我转到SQL查询窗口并手动将值更改为“已更新”。 然后我继续跑步。 Linq不重新加载我的对象,但重新使用它以前在内存中的对象! […]

ConcurrentDictionary替代可移植类库

我正在编写一个面向.NET 4.5,Windowsapp store应用程序和Windows Phone 8的可移植类库。我需要一个高效的内存缓存机制,因此我考虑使用ConcurrentDictionary ,但它不可用于WP8。 会有很多读取和相对较少的写入,所以理想情况下我想要一个支持来自多个线程的无锁读取的集合,并由单个线程写入。 根据MSDN ,非通用Hashtable具有该属性,但不幸的是它在PCL中不可用… PCL中是否有另一个符合此要求的集合类? 如果没有,那么在不锁定读取的情况下实现线程安全的好方法是什么? (锁定写入是可以的,因为它不会经常发生) 编辑:感谢JaredPar的指导,我最终使用来自Microsoft.Bcl.Immutable的 ImmutableDictionary以完全无锁的方式实现了我的缓存: class Cache { private IImmutableDictionary _cache = ImmutableDictionary.Create(); public TValue GetOrAdd(TKey key, [NotNull] Func valueFactory) { valueFactory.CheckArgumentNull(“valueFactory”); TValue newValue = default(TValue); bool newValueCreated = false; while (true) { var oldCache = _cache; TValue value; if (oldCache.TryGetValue(key, out value)) return value; // […]