Namespaces
Variants

Standard library header <thread> (C++11)

From cppreference.net
Standard library headers

Этот заголовок является частью библиотеки поддержки потоков .

Содержание

Включения

(C++20)
Поддержка оператора трёхстороннего сравнения

Пространства имён

this_thread предоставляет функции для доступа к текущему потоку выполнения

Классы

(C++11)
управляет отдельным потоком
(класс)
(C++20)
std::thread с поддержкой автоматического присоединения и отмены
(класс)
специализирует std::hash
(специализация шаблона класса)

Функции

специализирует алгоритм std::swap
(функция)
(удалено в C++20) (удалено в C++20) (удалено в C++20) (удалено в C++20) (удалено в C++20) (C++20)
сравнивает два объекта thread::id
(функция)
сериализует объект thread::id
(шаблон функции)
Определено в пространстве имён std::this_thread
(C++11)
предлагает реализации перепланировать выполнение потоков
(функция)
(C++11)
возвращает идентификатор текущего потока
(функция)
(C++11)
останавливает выполнение текущего потока на указанный промежуток времени
(функция)
останавливает выполнение текущего потока до указанной точки времени
(функция)

Синопсис

#include <compare>
namespace std {
  // класс thread
  class thread;
  void swap(thread& x, thread& y) noexcept;
  // класс jthread
  class jthread;
  // пространство имен this_thread
  namespace this_thread {
    thread::id get_id() noexcept;
    void yield() noexcept;
    template<class Clock, class Duration>
    void sleep_until(const chrono::time_point<Clock, Duration>& abs_time);
    template<class Rep, class Period>
    void sleep_for(const chrono::duration<Rep, Period>& rel_time);
  }
}

Класс std::thread

namespace std {
  class thread
  {
  public:
    // класс thread::id
    class id;
    using native_handle_type = /* implementation-defined */;
    // конструкторы/копирование/уничтожение
    thread() noexcept;
    template<class F, class... Args>
    explicit thread(F&& f, Args&&... args);
    ~thread();
    thread(const thread&) = delete;
    thread(thread&&) noexcept;
    thread& operator=(const thread&) = delete;
    thread& operator=(thread&&) noexcept;
    // методы
    void swap(thread&) noexcept;
    bool joinable() const noexcept;
    void join();
    void detach();
    id get_id() const noexcept;
    native_handle_type native_handle();
    // статические методы
    static unsigned int hardware_concurrency() noexcept;
  };
}

Класс std::jthread

namespace std {
  class jthread
  {
  public:
    // типы
    using id                 = thread::id;
    using native_handle_type = thread::native_handle_type;
    // конструкторы, перемещение и присваивание
    jthread() noexcept;
    template<class F, class... Args>
    explicit jthread(F&& f, Args&&... args);
    ~jthread();
    jthread(const jthread&) = delete;
    jthread(jthread&&) noexcept;
    jthread& operator=(const jthread&) = delete;
    jthread& operator=(jthread&&) noexcept;
    // методы
    void swap(jthread&) noexcept;
    bool joinable() const noexcept;
    void join();
    void detach();
    id get_id() const noexcept;
    native_handle_type native_handle();
    // работа с токеном остановки
    stop_source get_stop_source() noexcept;
    stop_token get_stop_token() const noexcept;
    bool request_stop() noexcept;
    // специализированные алгоритмы
    friend void swap(jthread& lhs, jthread& rhs) noexcept;
    // статические методы
    static unsigned int hardware_concurrency() noexcept;
  private:
    stop_source ssource; // только для демонстрации
  };
}

Класс std::thread::id

namespace std {
  class thread::id
  {
  public:
    id() noexcept;
  };
  bool operator==(thread::id x, thread::id y) noexcept;
  strong_ordering operator<=>(thread::id x, thread::id y) noexcept;
  template<class CharT, class Traits>
  basic_ostream<CharT, Traits>& operator<<(basic_ostream<CharT, Traits>& out,
                                           thread::id id);
  template<class CharT>
  struct formatter<thread::id, CharT>;
  // поддержка хеширования
  template<class T>
  struct hash;
  template<>
  struct hash<thread::id>;
}