:github_url: https://github.com/pytorch/pytorch .. _program_listing_file_torch_csrc_api_include_torch_data_detail_queue.h: Program Listing for File queue.h ================================ |exhale_lsh| :ref:`Return to documentation for file ` (``torch/csrc/api/include/torch/data/detail/queue.h``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp #pragma once #include #include #include #include #include #include #include namespace torch::data::detail { template class Queue { public: void push(T value) { { std::lock_guard lock(mutex_); queue_.push(std::move(value)); } cv_.notify_one(); } T pop(std::optional timeout = std::nullopt) { std::unique_lock lock(mutex_); if (timeout) { if (!cv_.wait_for( lock, *timeout, [this] { return !this->queue_.empty(); })) { // clang-format off TORCH_CHECK(false, "Timeout in DataLoader queue while waiting for next batch" " (timeout was ", timeout->count(), " ms)"); // clang-format on } } else { cv_.wait(lock, [this] { return !this->queue_.empty(); }); } AT_ASSERT(!queue_.empty()); T value = queue_.front(); queue_.pop(); lock.unlock(); return value; } size_t clear() { std::lock_guard lock(this->mutex_); const auto size = queue_.size(); while (!queue_.empty()) { queue_.pop(); } return size; } private: std::queue queue_; std::mutex mutex_; std::condition_variable cv_; }; } // namespace torch::data::detail