英特尔Inspector报告我在自旋锁实现中的数据竞争 [英] Intel Inspector reports a data race in my spinlock implementation

查看:120
本文介绍了英特尔Inspector报告我在自旋锁实现中的数据竞争的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Windows中的互锁功能制作了一个非常简单的自旋锁,并在双核CPU(增加一个变量的两个线程)上测试它。

I made a very simple spinlock using the Interlocked functions in Windows and tested it on a dual-core CPU (two threads that increment a variable);

程序似乎工作正常(每次都给出相同的结果,而不使用同步时不是这样),但是Intel Parallel Inspector 说在 value + = j 处有一个竞争条件(见下面的代码)。当使用Critical Sections而不是我的SpinLock时,警告消失。

The program seems to work OK (it gives the same result every time, which is not the case when no synchronization is used), but Intel Parallel Inspector says that there is a race condition at value += j (see the code below). The warning disappears when using Critical Sections instead of my SpinLock.

我的SpinLock实现是否正确?这真的很奇怪,因为所有使用的操作都是原子操作并且具有适当的内存障碍,并且不应导致竞争条件。

Is my implementation of SpinLock correct or not ? It's really strange, because all the used operations are atomic and have the proper memory barriers and it shouldn't lead to race conditions.

class SpinLock
{
   int *lockValue;
   SpinLock(int *value) : lockValue(value) { }

   void Lock() {
      while(InterlockedCompareExchange((volatile LONG*)lockValue, 1, 0) != 0) {
          WaitABit();
      }
   }

   void Unlock() { InterlockedExchange((volatile LONG*)lockValue, 0); }
};

测试程序:

static const int THREADS = 2;
HANDLE completedEvents[THREADS];
int value = 0;
int lock = 0; // Global.

DWORD WINAPI TestThread(void *param) {
    HANDLE completed = (HANDLE)param;
    SpinLock testLock(&lock);

    for(int i = 0;i < 1000*20; i++) {
        for(int j = 0;j < 10*10; j++) {
            // Add something to the variable.
            testLock.Lock();
            value += j;
            testLock.Unlock();
        }
    }
    SetEvent(completed);
}

int main() {
   for(int i = 0; i < THREADS; i++) {
        completedEvents[i] = CreateEvent(NULL, true, false, NULL);
   }
   for(int i = 0; i < THREADS; i++) {
        DWORD id;
        CreateThread(NULL, 0, TestThread, completedEvents[i], 0, &id);
   }

   WaitForMultipleObjects(THREADS, completedEvents, true, INFINITE);
   cout<<value;
}


推荐答案

Parallel Inspector的数据竞赛建议使用关键部分或互斥体在Windows上修复比赛。其中没有任何内容表明并行检查器知道如何识别您可能发明的任何其他锁定机制。

Parallel Inspector's documentation for data race suggests using a critical section or a mutex to fix races on Windows. There's nothing in it which suggests that Parallel Inspector knows how to recognise any other locking mechanism you might invent.

用于分析新型锁定机制的工具往往是静态工具,它们通过代码查看每个可能的路径,Parallel Inspector的文档意味着它执行代码一次。

Tools for analysis of novel locking mechanisms tend to be static tools which look at every possible path through the code, Parallel Inspector's documentation implies that it executes the code once.

如果你想尝试新颖的锁定机制,我在学术文献中使用的最常见的工具是自旋模型检查器。还有 ESP ,这可能会减少状态空间,但我不知道它是否已应用于并发问题,以及移动工作台这将给出一个分析,如果你可以在pi-calculus沙发你的问题。英特尔并行检查器似乎没有像这些工具那么复杂,而是旨在使用启发式检查常见问题。

If you want to experiment with novel locking mechanisms, the most common tool I've seen used in academic literature is the Spin model checker. There's also ESP, which might reduce the state space, but I don't know if it's been applied to concurrent problems, and also the mobility workbench which would give an analysis if you can couch your problem in pi-calculus. Intel Parallel Inspector doesn't seem anything like as complicated as these tools, but rather designed to check for commonly occurring issues using heuristics.

这篇关于英特尔Inspector报告我在自旋锁实现中的数据竞争的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆