这个函数可能会导致线程无限期等待吗?

问题描述 投票:0回答:1

我正在阅读flutter引擎中的代码

https://github.com/flutter/engine/blob/main/shell/common/platform_view.cc#L58

我想问一下,如果在

latch.Signal()
之前到达
latch.Wait()
,会无限等待吗?如果这种情况永远不会发生,如何保证?

void PlatformView::NotifyCreated() {
  std::unique_ptr<Surface> surface;
  // Threading: We want to use the platform view on the non-platform thread.
  // Using the weak pointer is illegal. But, we are going to introduce a latch
  // so that the platform view is not collected till the surface is obtained.
  auto* platform_view = this;
  fml::ManualResetWaitableEvent latch;
  fml::TaskRunner::RunNowOrPostTask(
      task_runners_.GetRasterTaskRunner(), [platform_view, &surface, &latch]() {
        surface = platform_view->CreateRenderingSurface();
        if (surface && !surface->IsValid()) {
          surface.reset();
        }
        latch.Signal();
      });
  latch.Wait();
  if (!surface) {
    FML_LOG(ERROR) << "Failed to create platform view rendering surface";
    return;
  }
  delegate_.OnPlatformViewCreated(std::move(surface));
}

解释代码细节以及有关它使用的互斥体的更多信息。分析 CPU 调度的可能性。

c++ multithreading thread-safety mutex flutter-engine
1个回答
1
投票

如果你看看它当前的实现 https://github.com/cloudwebrtc/flutter-engine/blob/master/fml/synchronization/waitable_event.cc

锁存器有一个布尔值,它在收到信号时设置,然后当稍后发生

wait
时,它会看到布尔值已经设置并且不会等待。

© www.soinside.com 2019 - 2024. All rights reserved.