thread-safety 相关问题

如果一段代码仅以允许多个线程一致执行此代码的方式操作数据结构,则它是线程安全的。代码可以是线程安全的,有条件安全的(需要互斥)或不安全(只能由一个线程安全使用)。

使用CAS&no Locks的线程安全汇款

我正在尝试以线程安全的方式解决将钱从一个帐户转移到另一个帐户的问题,因为这些帐户仅存在于内存中。 我能够轻松解决...

回答 1 投票 0

通过值/副本将本地向量转到线程

我的理解是,当使用

回答 0 投票 0



正确的术语等效于“线程安全”,但相对于多个数据库连接?

这仅仅是线程安全的另一个示例(例如,在较低级别的螺纹锁定以防止记忆的写入/突变)?还是还有另一个术语?

回答 1 投票 0






执行在暂停其他线程后粘在ManualResetevent.Set上。 我创建了一个工具,可以找到并恢复特定过程的悬挂线(该工具背后的原因是无关紧要的)。为了测试该工具,我创建了以下单元测试: // 安排 var

// Arrange var timeout = TimeSpan.FromSeconds(10); var locker = new object(); var suspendEvent = new ManualResetEventSlim(false); ulong threadId = 0; var thread = new Thread(() => { lock (locker) { threadId = InteropAPI.TRGetCurrentThreadId(); } Log($"Thread started: {threadId}"); suspendEvent.Wait(); Log($"Thread finished: {threadId}"); }); thread.Start(); for (var i = 0; i < 100; i++) { lock (locker) { if (threadId != 0) { break; } } Thread.Sleep(10); } Assert.That(threadId, Is.Not.EqualTo(0)); // Act Log("Suspending thread..."); InteropAPI.TRSuspendThread(threadId); Log("Thread suspended"); suspendEvent.Set(); Log("Event set"); // Assert part omitted

回答 1 投票 0

ConcurrentDictionary 陷阱 - GetOrAdd 和 AddOrUpdate 中的委托工厂是否同步?

ConcurrentDictionary 的文档没有明确说明,所以我想我们不能期望委托 valueFactory 和 updateValueFactory 的执行同步(来自 GetOrAdd() 和

回答 2 投票 0

`unique_ptr` 上的原子操作

std::shared_ptr 具有原子操作的专业化,例如atomic_compare_exchange_weak 和 family,但我找不到有关 std::unique_ptr 的等效专业化的文档。有没有...

回答 3 投票 0

SecureRandom 流在多线程中抛出异常

我正在尝试使用 SecureRandom 生成随机值,特别是它对流的支持。理想情况下,这些值应该不断生成,因此流可以是无限的: 安全随机

回答 1 投票 0

ConcurrentDictionary.GetOrAdd - 仅在不为空时添加

我使用 ConcurrentDictionary 来缓存并行访问的数据,有时新项目可以存储在数据库中,但不会加载到缓存中。这就是我使用 GetOrAdd 的原因: 公共用户 GetU...

回答 3 投票 0

ConcurrentDictionary.AddOrUpdate,将更新过时的值?

我对 AddOrUpdate 方法感到困惑。文档中特别提到了updateValueFactory是不同步的。 MSDN 中给出了这个例子: 并行.For(0, 10000, i => { ...

回答 1 投票 0

AddOrUpdate 时锁定 ConcurrentDictionary?

我使用 ConcurrentDictioanry> 跨多个线程访问一些数据。 我在这篇文章(向下滚动)中读到,方法 AddOrUpdate 未在 ...

回答 5 投票 0

ConcurrentDictionary 中的 AddOrUpdate 线程安全吗?

我尝试在ConcurrentDictionary中使用AddOrUpdate方法。 从本页的“备注”部分 https://msdn.microsoft.com/en-us/library/dd287191(v=vs.110).aspx。它说 “但是,代表……

回答 1 投票 0

ConcurrentDictionary 对象 - 通过不同线程读取和写入

我想在我的应用程序中使用 ConcurrentDictionary,但首先我需要确保我正确理解它是如何工作的。在我的应用程序中,我将有一个或多个线程写入字典或从中删除......

回答 2 投票 0

将不同的值添加到并发字典内的列表中

我想将小数添加到 ConcurrentDictionary 内的列表中 ConcurrentDictionary> fullList = 新的并发词典 我想将小数添加到 ConcurrentDictionary 内的列表中 ConcurrentDictionary<string, List<decimal>> fullList = new ConcurrentDictionary<string, List<decimal>>(); public void AddData(string key, decimal value) { if (fullList.ContainsKey(key)) { var partialList = fullList[key]; partialList.Add(value); } else { fullList.GetOrAdd(key, new List<decimal>() { value }); } } 从技术上讲,上面的代码对我有用,但只是这样做,因为我不知道如何执行 GetOrAdd 方法来添加和更新。我的问题是,考虑到我的更新将在现有列表的末尾添加一个项目,我该如何使用该方法? 您可以使用 AddOrUpdate 方法来达到此目的,如下所示: fullList.AddOrUpdate(key, new List<decimal>() { value }, (key,list) => { list.Add(value); return list; }); 本质上,当 key missing 时,会创建一个仅包含一个元素 value 的新列表。否则,我们将 key 添加到与当前 value 关联的列表中,然后返回该列表。 有关此方法的文档,请查看这里。 您在寻找这样的东西吗?希望有帮助。 ConcurrentDictionary<string, List<decimal>> fullList = new ConcurrentDictionary<string, List<decimal>>(); public void AddData(string key, decimal value) { List<decimal> list; if (!fullList.TryGetValue(key, out list)) { list = new List<decimal>(); fullList.GetOrAdd(key, list); } list.Add(value); } 您只需要在所有情况下调用GetOrAdd: public void AddData(string key, decimal value) { var partialList = fullList.GetOrAdd(key, _ => new List<decimal>()); lock (partialList) partialList.Add(value); // Lock for thread safety } 当您使用 ConcurrentDictionary 时,您应该努力使用其特殊的并发 API,以强制执行操作的原子性。分两步检查和添加 (ContainsKey+Add) 不是原子的,它会引入竞争条件,并可能损害应用程序的正确性。

回答 3 投票 0

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.