Shortcuts

Template Class Queue

Page Contents

Class Documentation

template<typename T>
class Queue

A basic locked, blocking MPMC queue.

Every push and pop is guarded by a mutex. A condition variable is used to communicate insertion of new elements, such that waiting threads will be woken up if they are currently waiting inside a call to pop().

Note that this data structure is written specifically for use with the DataLoader. Its behavior is tailored to this use case and may not be applicable to more general uses.

Public Functions

inline void push(T value)

Pushes a new value to the back of the Queue and notifies one thread on the waiting side about this event.

inline T pop(std::optional<std::chrono::milliseconds> timeout = std::nullopt)

Blocks until at least one element is ready to be popped from the front of the queue.

An optional timeout in seconds can be used to limit the time spent waiting for an element. If the wait times out, an exception is raised.

inline size_t clear()

Empties the queue and returns the number of elements that were present at the start of the function.

No threads are notified about this event as it is assumed to be used to drain the queue during shutdown of a DataLoader.

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources