当前位置: 首页>>代码示例>>C++>>正文


C++ EventBase::runInEventBaseThreadAndWait方法代码示例

本文整理汇总了C++中EventBase::runInEventBaseThreadAndWait方法的典型用法代码示例。如果您正苦于以下问题:C++ EventBase::runInEventBaseThreadAndWait方法的具体用法?C++ EventBase::runInEventBaseThreadAndWait怎么用?C++ EventBase::runInEventBaseThreadAndWait使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在EventBase的用法示例。


在下文中一共展示了EventBase::runInEventBaseThreadAndWait方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: atoms

//  This test simulates some calls, and verifies that the waiting happens by
//  triggering what otherwise would be race conditions, and trying to detect
//  whether any of the race conditions happened.
TEST(EventBaseTest, RunInEventBaseThreadAndWait) {
  const size_t c = 256;
  vector<unique_ptr<atomic<size_t>>> atoms(c);
  for (size_t i = 0; i < c; ++i) {
    auto& atom = atoms.at(i);
    atom = make_unique<atomic<size_t>>(0);
  }
  vector<thread> threads(c);
  for (size_t i = 0; i < c; ++i) {
    auto& atom = *atoms.at(i);
    auto& th = threads.at(i);
    th = thread([&atom] {
        EventBase eb;
        auto ebth = thread([&]{ eb.loopForever(); });
        eb.waitUntilRunning();
        eb.runInEventBaseThreadAndWait([&] {
          size_t x = 0;
          atom.compare_exchange_weak(
              x, 1, std::memory_order_release, std::memory_order_relaxed);
        });
        size_t x = 0;
        atom.compare_exchange_weak(
            x, 2, std::memory_order_release, std::memory_order_relaxed);
        eb.terminateLoopSoon();
        ebth.join();
    });
  }
  for (size_t i = 0; i < c; ++i) {
    auto& th = threads.at(i);
    th.join();
  }
  size_t sum = 0;
  for (auto& atom : atoms) sum += *atom;
  EXPECT_EQ(c, sum);
}
开发者ID:NextGenIntelligence,项目名称:folly,代码行数:38,代码来源:EventBaseTest.cpp

示例2: th

TEST(EventBaseTest, RunImmediatelyOrRunInEventBaseThreadAndWaitWithin) {
  EventBase eb;
  thread th(&EventBase::loopForever, &eb);
  SCOPE_EXIT {
    eb.terminateLoopSoon();
    th.join();
  };
  eb.runInEventBaseThreadAndWait([&] {
      auto mutated = false;
      eb.runImmediatelyOrRunInEventBaseThreadAndWait([&] {
          mutated = true;
      });
      EXPECT_TRUE(mutated);
  });
}
开发者ID:NextGenIntelligence,项目名称:folly,代码行数:15,代码来源:EventBaseTest.cpp


注:本文中的EventBase::runInEventBaseThreadAndWait方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。