Tag: multithreading

内存障碍是否可以保证C#的新读取?

如果我们在C#中有以下代码: int a = 0; int b = 0; void A() // runs in thread A { a = 1; Thread.MemoryBarrier(); Console.WriteLine(b); } void B() // runs in thread B { b = 1; Thread.MemoryBarrier(); Console.WriteLine(a); } MemoryBarriers确保写入指令在读取之前发生。 但是,是否可以保证在另一个线程上读取一个线程的写入? 换句话说,是否保证至少有一个线程打印1或两个线程都可以打印0 ? 我知道已经存在几个与C#中的“ MemoryBarrier ”和MemoryBarrier相关的问题,就像这样和这个 。 但是,它们中的大多数都处理写入释放和读取 – 获取模式。 在这个问题中发布的代码,非常特定于在保持指令保持有序的情况下是否保证通过读取来查看写入。

同步线程协同程序

我试图让线程在之前等待对方,以便保持同步。 在我的实际程序中,我有很多IObjectObserved对象(在他们自己的线程上)发送事件,我想保持所有内容同步,所以IObjectListener (在它自己的线程上)可以监听这些对象之一50次然后订阅另一个及时赶上第51届活动。 我还没有那么远,但我认为同步线程是主要问题。 我设法通过使用AutoResetEvent的双向信令来实现这一点。 有没有更好的方法来做到这一点? class Program { static EventWaitHandle _ready = new AutoResetEvent(true); static EventWaitHandle _go = new AutoResetEvent(false); static EventWaitHandle _ready1 = new AutoResetEvent(true); static EventWaitHandle _go1 = new AutoResetEvent(false); static EventWaitHandle _ready2 = new AutoResetEvent(true); static EventWaitHandle _go2 = new AutoResetEvent(false); static void Main(string[] args) { new Thread(Waiter).Start(); new Thread(Waiter1).Start(); new […]

奇怪的系统.__佳能exception

我有一个使用单例类ThreadQueue的Windows服务。 当服务启动时,它调用ThreadQueue.Start()然后该类接受并将限制并发的任务排队到可配置数量的线程。 ThreadQueue.Start()只在服务启动时调用一次。 有时,在运行几个小时后,我收到以下exception: Application: myservice.exe Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception. Exception Info: System.NullReferenceException Stack: at Apollo.Business.Framework.Threading.ThreadQueue.ThreadQueue`1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].Start() at System.Threading.ThreadHelper.ThreadStart_Context(System.Object) at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object) at System.Threading.ThreadHelper.ThreadStart() 什么是System.__Canon和什么使这个调用作为类型参数传递给它? 谁能摆脱任何光明?

内部控件上的“跨线程操作无效”exception

我一直在努力解决这个问题:我有一个function,旨在将控件添加到具有跨线程处理的面板,问题是虽然面板和控件在“InvokeRequired = false” – 我得到一个exception告诉我其中一个控件内部控件是从其创建的线程以外的线程访问的,该代码片段如下所示: public delegate void AddControlToPanelDlgt(Panel panel, Control ctrl); public void AddControlToPanel(Panel panel, Control ctrl) { if (panel.InvokeRequired) { panel.Invoke(new AddControlToPanelDlgt(AddControlToPanel),panel,ctrl); return; } if (ctrl.InvokeRequired) { ctrl.Invoke(new AddControlToPanelDlgt(AddControlToPanel),panel,ctrl); return; } panel.Controls.Add(ctrl); //<– here is where the exception is raised } exception消息如下: “跨线程操作无效:控制’pnlFoo’从其创建的线程以外的线程访问” (’pnlFoo’在ctrl.Controls下) 如何将ctrl添加到面板?! 当代码到达“panel.Controls.Add(ctrl);”时 line – panel和ctrl“InvokeRequired”属性设置为false,问题是ctrl中的控件将“InvokeRequired”设置为true。 澄清事情:在基本线程上创建“panel”,在新线程上创建“ctrl”,因此,必须调用“panel”(导致“ctrl”再次需要调用)。 一旦完成两个调用,它就会到达panel.Controls.Add(ctrl)命令(“panel”和“ctrl”都不需要在这种状态下调用) 这是完整程序的一小部分: public […]

仅在值相同时锁定?

如果我有一个在我的webservice中编辑数据库的函数,我只想让一个线程一次执行,如果他们试图编辑同一行。 void EditCheck(long checkid) { if (isCheckCosed) throw new Exception(“check is already closed”); //do stuff //after i edit my check i want to close it. CloseCheck(); } 我知道我可以锁定整个function,但随后我松开了性能,因为它几乎从来没有那个不同的线程会尝试编辑相同的检查。 有没有办法只锁定具有相同checkid的其他线程? UPDATE 我使用OleDbConnection和MySqlConnection OleDbCommand oleDbCommand = AccessTrans != null ? new OleDbCommand(sql, AccessConn, AccessTrans) : new OleDbCommand(sql, AccessConn); oleDbCommand.ExecuteNonQuery(); 与MySqlCommand相同的function 然后我使用正常的INSERT和UPDATE sql命令。 并检查交易是否存在。 所以如果您想在上级function中进行交易,这个function是有效的。 从数据库中读取我将填写DataTable OleDbCommand oleDbCommand […]

在HashSet 中包含线程安全

查看.NET源代码中HashSet类中Contains的代码,我找不到Contains不是线程安全的原因吗? 我正在提前使用值加载HashSet ,然后在multithreading中检查Contains 。 AsParallel()循环。 这有什么理由不安全吗? 当我实际上不需要存储值时,我不愿意使用ConcurrentDictionary 。

C#数组中的线程安全性

有2个不同的线程: 一个从C#数组读取(例如从第一个位置读取), 另一个写入相同的C#数组但写入不同的位置(例如到最后一个位置) 线程安全与否? (我的意思是这里没有锁定阅读或写作)

C#unit testing – Thread.Sleep(x) – 如何模拟系统时钟

我必须测试一个间隔后做一定工作量的方法。 while (running) { … // Work … Thread.Sleep(Interval); } Interval作为参数传递给类,所以我可以传入0或1,但我对如何模拟系统时钟感兴趣,如果不是这样的话。 在我的测试中,我希望能够通过TimeSpan Interval简单地设置时间并让线程唤醒。 我从来没有编写过代码,这些代码之前会对执行的线程起作用,我确信有一些缺陷需要避免 – 请随意详细说明你使用的方法。 谢谢!

Singleton的简单实现

这不是一个更简单,更安全(因此更好)的方式来实现单例而不是双重检查锁定mambo-jambo? 这种方法有什么缺点吗? public class Singleton { private static Singleton _instance; private Singleton() { Console.WriteLine(“Instance created”); } public static Singleton Instance { get { if (_instance == null) { Interlocked.CompareExchange(ref _instance, new Singleton(), null); } return _instance; } } public void DoStuff() { } } 编辑:线程安全测试失败,谁能解释为什么? 为什么Interlocked.CompareExchange不是真正的primefaces? public class Program { static void Main(string[] args) { […]

任务继续与async / await并行执行

在使用async / await结构的控制台应用程序的上下文中,我想知道“continuation”是否可以在不同CPU上的多个线程上并行运行。 我认为就是这种情况,因为延迟发布在默认任务调度程序(控制台应用程序中没有SynchronizationContext)上,这是线程池。 我知道async / await构造不构造任何额外的线程。 仍然应该由线程池为每个CPU构建至少一个线程,因此如果在线程池上发布了continuation,它可以在不同的CPU上并行调度任务延续…这就是我的想法,但出于某种原因我昨天对此我真的很困惑,我不再那么肯定了。 这是一些简单的代码: public class AsyncTest { int i; public async Task DoOpAsync() { await SomeOperationAsync(); // Does the following code continuation can run // in parrallel ? i++; // some other continuation code …. } public void Start() { for (int i=0; i<1000; i++) { var _ = […]