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


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

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


在下文中一共展示了EventBase::terminateLoopSoon方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: cb

TEST(EventBaseTest, RunBeforeLoop) {
  EventBase base;
  CountedLoopCallback cb(&base, 1, [&](){
    base.terminateLoopSoon();
  });
  base.runBeforeLoop(&cb);
  base.loopForever();
  ASSERT_EQ(cb.getCount(), 0);
}
开发者ID:NextGenIntelligence,项目名称:folly,代码行数:9,代码来源:EventBaseTest.cpp

示例3: async_tm_update

 void async_tm_update(unique_ptr<HandlerCallback<int32_t>> callback,
                      int32_t currentIndex) override {
   auto callbackp = callback.release();
   EXPECT_EQ(currentIndex, expectIndex_);
   expectIndex_++;
   EventBase *eb = callbackp->getEventBase();
   callbackp->resultInThread(currentIndex);
   if (expectIndex_ == lastIndex_) {
     success_ = true;
     eb->runInEventBaseThread([eb] { eb->terminateLoopSoon(); });
   }
 }
开发者ID:adityavs,项目名称:fbthrift,代码行数:12,代码来源:DuplexTest.cpp

示例4: th

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

示例5: TestData

TEST(RequestContext, SimpleTest) {
  EventBase base;


  // There should always be a default context with get()
  EXPECT_TRUE(RequestContext::get() != nullptr);


  // but not with saveContext()
  EXPECT_EQ(RequestContext::saveContext(), nullptr);
  RequestContext::create();
  EXPECT_NE(RequestContext::saveContext(), nullptr);
  RequestContext::create();
  EXPECT_NE(RequestContext::saveContext(), nullptr);

  EXPECT_EQ(nullptr, RequestContext::get()->getContextData("test"));

  RequestContext::get()->setContextData(
    "test",
    std::unique_ptr<TestData>(new TestData(10)));
  base.runInEventBaseThread([&](){
      EXPECT_TRUE(RequestContext::get() != nullptr);
      auto data = dynamic_cast<TestData*>(
        RequestContext::get()->getContextData("test"))->data_;
      EXPECT_EQ(10, data);
      base.terminateLoopSoon();
    });
  auto th = std::thread([&](){
      base.loopForever();
  });
  th.join();
  EXPECT_TRUE(RequestContext::get() != nullptr);
  auto a = dynamic_cast<TestData*>(
    RequestContext::get()->getContextData("test"));
  auto data = a->data_;
  EXPECT_EQ(10, data);

  RequestContext::setContext(std::shared_ptr<RequestContext>());
  // There should always be a default context
  EXPECT_TRUE(nullptr != RequestContext::get());
}
开发者ID:BocaiFire,项目名称:folly,代码行数:41,代码来源:RequestContextTest.cpp

示例6:

/**
 * Test that thisLoop functionality works with terminateLoopSoon
 */
TEST(EventBaseTest, ThisLoop) {
  EventBase eb;
  bool runInLoop = false;
  bool runThisLoop = false;

  eb.runInLoop([&](){
      eb.terminateLoopSoon();
      eb.runInLoop([&]() {
          runInLoop = true;
        });
      eb.runInLoop([&]() {
          runThisLoop = true;
        }, true);
    }, true);
  eb.loopForever();

  // Should not work
  ASSERT_FALSE(runInLoop);
  // Should work with thisLoop
  ASSERT_TRUE(runThisLoop);
}
开发者ID:NextGenIntelligence,项目名称:folly,代码行数:24,代码来源:EventBaseTest.cpp

示例7: handler

TEST(EventBaseTest, StopBeforeLoop) {
  EventBase evb;

  // Give the evb something to do.
  int p[2];
  ASSERT_EQ(0, pipe(p));
  PipeHandler handler(&evb, p[0]);
  handler.registerHandler(EventHandler::READ);

  // It's definitely not running yet
  evb.terminateLoopSoon();

  // let it run, it should exit quickly.
  std::thread t([&] { evb.loop(); });
  t.join();

  handler.unregisterHandler();
  close(p[0]);
  close(p[1]);

  SUCCEED();
}
开发者ID:NextGenIntelligence,项目名称:folly,代码行数:22,代码来源:EventBaseTest.cpp

示例8: maxReadAtOnce

void QueueTest::maxReadAtOnce() {
  // Add 100 messages to the queue
  for (int n = 0; n < 100; ++n) {
    queue.putMessage(n);
  }

  EventBase eventBase;

  // Record how many messages were processed each loop iteration.
  uint32_t messagesThisLoop = 0;
  std::vector<uint32_t> messagesPerLoop;
  std::function<void()> loopFinished = [&] {
    // Record the current number of messages read this loop
    messagesPerLoop.push_back(messagesThisLoop);
    // Reset messagesThisLoop to 0 for the next loop
    messagesThisLoop = 0;

    // To prevent use-after-free bugs when eventBase destructs,
    // prevent calling runInLoop any more after the test is finished.
    // 55 == number of times loop should run.
    if (messagesPerLoop.size() != 55) {
      // Reschedule ourself to run at the end of the next loop
      eventBase.runInLoop(loopFinished);
    }
  };
  // Schedule the first call to loopFinished
  eventBase.runInLoop(loopFinished);

  QueueConsumer consumer;
  // Read the first 50 messages 10 at a time.
  consumer.setMaxReadAtOnce(10);
  consumer.fn = [&](int value) {
    ++messagesThisLoop;
    // After 50 messages, drop to reading only 1 message at a time.
    if (value == 50) {
      consumer.setMaxReadAtOnce(1);
    }
    // Terminate the loop when we reach the end of the messages.
    if (value == 99) {
      eventBase.terminateLoopSoon();
    }
  };
  consumer.startConsuming(&eventBase, &queue);

  // Run the event loop until the consumer terminates it
  eventBase.loop();

  // The consumer should have read all 100 messages in order
  EXPECT_EQ(100, consumer.messages.size());
  for (int n = 0; n < 100; ++n) {
    EXPECT_EQ(n, consumer.messages.at(n));
  }

  // Currently EventBase happens to still run the loop callbacks even after
  // terminateLoopSoon() is called.  However, we don't really want to depend on
  // this behavior.  In case this ever changes in the future, add
  // messagesThisLoop to messagesPerLoop in loop callback isn't invoked for the
  // last loop iteration.
  if (messagesThisLoop > 0) {
    messagesPerLoop.push_back(messagesThisLoop);
    messagesThisLoop = 0;
  }

  // For the first 5 loops it should have read 10 messages each time.
  // After that it should have read 1 messages per loop for the next 50 loops.
  EXPECT_EQ(55, messagesPerLoop.size());
  for (int n = 0; n < 5; ++n) {
    EXPECT_EQ(10, messagesPerLoop.at(n));
  }
  for (int n = 5; n < 55; ++n) {
    EXPECT_EQ(1, messagesPerLoop.at(n));
  }
}
开发者ID:GYGit,项目名称:folly,代码行数:73,代码来源:NotificationQueueTest.cpp


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