多线程函数中的访问冲突读取位置错误

Access violation reading location error in multithread function

请检查此代码是否存在多线程错误。有时我在第 cv.notify_all(); 行(在代码中标记)出现错误“访问违规读取位置”。

uint64_t GetFirstAdvertisingBLEAddr()
{
    std::mutex m;
    std::condition_variable cv;

    BluetoothLEAdvertisementWatcher advWatcher;
    uint64_t addr = 0;
    bool stopCalled = false;
    auto recvToken = advWatcher.Received([&addr, &m, &cv](BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs) {
        ShowAdvertisement(eventArgs);
        addr = eventArgs.BluetoothAddress();
        watcher.Stop();
        std::unique_lock l(m);
        cv.notify_all(); // <----- HERE ------------------------------------
    });
    auto stoppedToken = advWatcher.Stopped([&stopCalled, &m, &cv](BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementWatcherStoppedEventArgs eventArgs) {
        std::unique_lock l(m);
        stopCalled = true;
        cv.notify_all();
    });

    {
        std::unique_lock l(m);
        advWatcher.Start();
        cv.wait(l, [&addr, &stopCalled, &advWatcher] { return addr != 0 && stopCalled && (advWatcher.Status() == BluetoothLEAdvertisementWatcherStatus::Aborted || advWatcher.Status() == BluetoothLEAdvertisementWatcherStatus::Stopped); });
    }

    // remove event handlers
    advWatcher.Received(recvToken);
    advWatcher.Stopped(stoppedToken);

    return addr;
}

如果您需要更多代码:

int main()
{
    winrt::init_apartment();

    std::wcout << "Main thread: " << std::this_thread::get_id() << std::endl;

    uint64_t addr = GetFirstAdvertisingBLEAddr();
    if (addr == 0) {
        std::wcout << L"Failed to find advertising BLE device." << std::endl;
        return 1;
    }
    std::wcout << "Found BLE device: " << GetDeviceName(addr) << std::endl;

    BluetoothLEDevice dev = BluetoothLEDevice::FromBluetoothAddressAsync(addr).get();

    GattDeviceServicesResult result = dev.GetGattServicesAsync(BluetoothCacheMode::Uncached).get();
.......

当函数 GetFirstAdvertisingBLEAddr returns 时,互斥锁和条件变量都超出范围。然后,您的 lambda 表达式中捕获的变量就会悬空,读取这些变量会使您的程序出现未定义的行为。

一种可能的解决方法是使它们 static:

static std::mutex m;
static std::condition_variable cv;