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


C++ TEventBase::runInEventBaseThread方法代码示例

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


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

示例1: TestData

TEST(RequestContext, SimpleTest) {
  TEventBase base;

  EXPECT_FALSE(RequestContext::create());
  EXPECT_TRUE(RequestContext::create());
  EXPECT_TRUE(RequestContext::get() != 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:Hsin,项目名称:fbthrift,代码行数:33,代码来源:RequestContextTest.cpp

示例2: 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_++;
   TEventBase *eb = callbackp->getEventBase();
   callbackp->resultInThread(currentIndex);
   if (expectIndex_ == lastIndex_) {
     success_ = true;
     eb->runInEventBaseThread([eb] { eb->terminateLoopSoon(); });
   }
 }
开发者ID:andrianyablonskyy,项目名称:fbthrift,代码行数:12,代码来源:SecurityTest.cpp

示例3: multiConsumer

void QueueTest::multiConsumer() {
  uint32_t numConsumers = 8;
  int numMessages = 10000;

  // Create several consumers each running in their own TEventBase thread
  vector<QueueConsumer> consumers(numConsumers);
  vector<ScopedEventBaseThread> threads(numConsumers);

  for (uint32_t consumerIdx = 0; consumerIdx < numConsumers; ++consumerIdx) {
    QueueConsumer* consumer = &consumers[consumerIdx];

    consumer->fn = [consumer, consumerIdx, this](int value) {
      // Treat 0 as a signal to stop.
      if (value == 0) {
        consumer->stopConsuming();
        // Put a message on the terminationQueue to indicate we have stopped
        terminationQueue.putMessage(consumerIdx);
      }
    };

    TEventBase* eventBase = threads[consumerIdx].getEventBase();
    eventBase->runInEventBaseThread([eventBase, consumer, this] {
      consumer->startConsuming(eventBase, &queue);
    });
  }

  // Now add a number of messages from this thread
  // Start at 1 rather than 0, since 0 is the signal to stop.
  for (int n = 1; n < numMessages; ++n) {
    queue.putMessage(n);
  }
  // Now add a 0 for each consumer, to signal them to stop
  for (int n = 0; n < numConsumers; ++n) {
    queue.putMessage(0);
  }

  // Wait until we get notified that all of the consumers have stopped
  // We use a separate notification queue for this.
  QueueConsumer terminationConsumer;
  vector<uint32_t> consumersStopped(numConsumers, 0);
  uint32_t consumersRemaining = numConsumers;
  terminationConsumer.fn = [&](int consumerIdx) {
    --consumersRemaining;
    if (consumersRemaining == 0) {
      terminationConsumer.stopConsuming();
    }

    BOOST_REQUIRE(consumerIdx >= 0);
    BOOST_REQUIRE(consumerIdx < numConsumers);
    ++consumersStopped[consumerIdx];
  };
  TEventBase eventBase;
  terminationConsumer.startConsuming(&eventBase, &terminationQueue);
  eventBase.loop();

  // Verify that we saw exactly 1 stop message for each consumer
  for (uint32_t n = 0; n < numConsumers; ++n) {
    BOOST_CHECK_EQUAL(consumersStopped[n], 1);
  }

  // Validate that every message sent to the main queue was received exactly
  // once.
  vector<int> messageCount(numMessages, 0);
  for (uint32_t n = 0; n < numConsumers; ++n) {
    for (int msg : consumers[n].messages) {
      BOOST_REQUIRE(msg >= 0);
      BOOST_REQUIRE(msg < numMessages);
      ++messageCount[msg];
    }
  }

  // 0 is the signal to stop, and should have been received once by each
  // consumer
  BOOST_CHECK_EQUAL(messageCount[0], numConsumers);
  // All other messages should have been received exactly once
  for (int n = 1; n < numMessages; ++n) {
    BOOST_CHECK_EQUAL(messageCount[n], 1);
  }
}
开发者ID:343829084,项目名称:fbthrift,代码行数:79,代码来源:TNotificationQueueTest.cpp


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