锁定集合的通常最佳方法是什么?

发布于 2024-12-05 10:10:50 字数 774 浏览 0 评论 0原文

假设我有一个跨多线程应用程序读取和写入的项目集合。当涉及到对某些项目应用算法时,我会采用不同的方式来获取锁。

通过在整个操作期间锁定:

lock(collection)
{
    for each thing in things
    {
        get the item from collection that matches thing
        do stuff with item
    }
}

通过按需锁定:

for each thing in things
{
    lock(collection)
    {
        get the item from collection that matches thing
    }
    do stuff with item
}

或者通过按需锁定来获取线程安全的项目集合以供稍后处理,从而使集合锁定更短的时间:

Items items
for each thing in things
{
    lock(collection)
    {
        get the item from collection that matches thing
    }
    items.Add(item)
}
for each item in items
{
    do stuff with item
}

我知道这最终可能取决于实际算法应用于每个项目,但是您会做什么?我正在使用 C++,但我很确定它是无关紧要的。

Suppose I have a collection of items which is read and written accross a multithreaded application. When it comes to apply an algorithm over some items I thing of different ways to acquire a lock.

By locking during the entire operation:

lock(collection)
{
    for each thing in things
    {
        get the item from collection that matches thing
        do stuff with item
    }
}

By locking on demand:

for each thing in things
{
    lock(collection)
    {
        get the item from collection that matches thing
    }
    do stuff with item
}

Or by locking on demand to get a thread safe collection of items to process later, thus having collection locked for a smaller amount of time:

Items items
for each thing in things
{
    lock(collection)
    {
        get the item from collection that matches thing
    }
    items.Add(item)
}
for each item in items
{
    do stuff with item
}

I know it may eventually depend on the actual algorithm applied to each item, but what would you do? I am using C++, but I am pretty sure it's irrelevant.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

羅雙樹 2024-12-12 10:10:50

看一下 双重检查锁定 模式,其中涉及下面的集合/字段的单独字段锁。

另外值得一看的是读者-作者锁定技术,该技术允许在阅读时同时阅读其他线程更新集合

编辑:
正如 David Heffernan 提到的,请查看 “双重检查锁定”破碎”宣言讨论

Take a look at the Double Check lock pattern which involves separate field for the collection/field under the lock.

Also it worth to take a look at the Readers-writer lock technique which allows reading whilst an other thread updating a collection

EDIT:
As David Heffernan mentioned take a look at the The "Double-Checked Locking is Broken" Declaration discussion

在梵高的星空下 2024-12-12 10:10:50

在多线程设置中,通过线程读取和写入,您的第一个和第二个示例具有不同的含义。如果“对项目执行某些操作”与其他线程交互,您的第三个示例可能还有另一个含义。

在决定如何执行之前,您需要先确定代码要执行的操作。

In a multi-threaded setting, with thread reading and writing, your first and second examples have different meaning. Your third example could have yet another meaning if "do something with item" interacts with other threads.

You need to decide what you want the code to do before deciding how to do it.

同展鸳鸯锦 2024-12-12 10:10:50

锁的获取和释放是昂贵的。我会锁定集合而不是循环中的每个单独元素。如果整个操作需要是原子的,这是有意义的。

Lock acquisition and release are expensive. I would lock over the collection rather than each individual element in a loop. It makes sense if the entire operation needs to be atomic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文