<thread>

[added with C++11]


get_id · hash · operator<< · operator== · operator!= · operator< · operator<= · operator> · operator>= · sleep_for · sleep_until · __STDCPP_THREADS__ · swap · thread · yield


Include the standard header <thread> to define the class thread and various supporting functions.

A thread object can be used to observe and manage a thread of execution within an application. A thread object created with the default constructor is not associated with any thread of execution. A thread object constructed with a callable object creates a new thread of execution and calls the callable object in that thread. Thread objects can be moved but not copied; thus, there can be only one thread object associated with any thread of execution.

Every thread of execution has a unique identifier of type thread::id. The function this_thread::get_id returns the identifier of the calling thread; the member function thread::get_id returns the identifier of the thread managed by a thread object. For a default-constructed thread object, the member function thread::get_id returns an object with a value that is the same for all default-constructed thread objects and different from the value returned by this_thread::get_id for any thread of execution that could be joined at the time of the call.

#define __STDCPP_THREADS__ 1
namespace std {
class thread;

template<size_t Bits>
    struct hash<thread::id>
        : public unary_function<bitset<Bits>, size_t> {
    size_t operator()(thread::id val) const;
    };

bool operator==(thread::id Left, thread::id Right) noexcept;
bool operator!=(thread::id Left, thread::id Right) noexcept;
bool operator< (thread::id Left, thread::id Right) noexcept;
bool operator<=(thread::id Left, thread::id Right) noexcept;
bool operator> (thread::id Left, thread::id Right) noexcept;
bool operator>=(thread::id Left, thread::id Right) noexcept;

void swap(thread& Left, thread& Right) noexcept;

template<class Elem, class Tr>
    basic_ostream<Elem, Tr>& operator<<(
        basic_ostream<Elem, Tr>& ostr, thread::id id);

    namespace this_thread {
    thread::id get_id() noexcept;

    void yield() noexcept;
    template<class Rep, class Period>
        void sleep_for(const chrono::duration<Rep, Period>& Rel_time);
    template<class Clock, class Duration>
        void sleep_until(const chrono::time_point<Clock, Duration>& Abs_time);
    void sleep_until(const xtime *Abs_time);
    }   // namespace this_thread
}   // namespace std

this_thread::get_id

thread::id get_id noexcept;

The function returns an object of type thread::id that uniquely identifies the current thread of execution.

hash

template<size_t Bits>
    struct hash<thread::id>
        : public unary_function<bitset<Bits>, size_t> {
    size_t operator()(thread::id val) const;
    };

The template class defines its member function as returning a value uniquely determined by val. The member function defines a hash function, suitable for mapping values of type thread::id to a distribution of index values.

operator<<

template<class Elem, class Tr>
    basic_ostream<Elem, Tr>& operator<<(
        basic_ostream<Elem, Tr>& ostr, thread::id id);

The template function overloads operator<< to insert a text representation of the object id of class thread::id into the stream ostr. If two objects of class thread::id compare equal, the inserted text representations for the two objects will be the same; if they do not compare equal, the inserted text representations will be different.

operator==

bool operator==(thread::id Left, thread::id Right) noexcept;

The template function overloads operator== to compare two objects of class thread::id. It returns true if the two objects represent the same thread of execution or if neither of the two objects represents a thread of execution, otherwise it returns false. It does not throw any exceptions.

operator!=

bool operator!=(thread::id Left, thread::id Right) noexcept;

The template function overloads operator!= to compare two objects of class thread::id. It returns !(Left == Right).

operator<

bool operator<(thread::id Left, thread::id Right) noexcept;

The template function overloads operator< to compare two objects of class thread::id. It defines a total ordering on all such objects; thus, objects of type thread::id can be used as keys in associative containers. It does not throw any exceptions.

operator<=

bool operator<=(thread::id Left, thread::id Right) noexcept;

The teplate function overloads operator<= to compare two objects of class thread::id. It returns !(Right < Left).

operator>

bool operator>(thread::id Left, thread::id Right) noexcept;

The teplate function overloads operator> to compare two objects of class thread::id. It returns Right < Left.

operator>=

bool operator>=(thread::id Left, thread::id Right) noexcept;

The teplate function overloads operator>= to compare two objects of class thread::id. It returns !(Left < Right).

this_thread::sleep_for

template<class Rep, class Period>
    void sleep_for(const chrono::duration<Rep, Period>& Rel_time);

The template function blocks the calling thread for at least the time specified by Rel_time. It does not throw any exceptions.

__STDCPP_THREADS__

#define __STDCPP_THREADS__ 1

The macro is defined as a nonzero value to indicate that threads are supported by this header.

this_thread::sleep_until

template<class Clock, class Duration>
    void sleep_until(const chrono::time_point<Clock, Duration>& Abs_time);
void sleep_until(const xtime *Abs_time);

The first template function blocks the calling thread at least until the time specified by Abs_time. It does not throw any exceptions.

In this implementation, the second function blocks the calling thread at least until the time specified by Abs_time. It does not throw any exceptions.

swap

void swap(thread& Left, thread& Right) noexcept;

The function calls Left.swap(Right).

thread


detach · get_id · hardware_concurrency · id · join · joinable · native_handle · native_handle_type · operator= · swap · thread · ~thread


namespace std {
    class thread
    {
public:
    struct id;

    thread() noexcept;
    template<class Fn, class... Args>
        explicit thread(Fn&& F, Args&&... A);
    thread(thread&& Other) noexcept;
    thread& operator=(thread&& Other) noexcept;
    ~thread();

    thread(const thread&) = delete;
    thread& operator=(const thread&) = delete;

    void swap(thread& Other) noexcept;

    bool joinable() const noexcept;
    void join();
    void detach();
    id get_id() const noexcept;

    static unsigned hardware_concurrency() noexcept;

    typedef h-type native_handle_type;
    native_handle_type native_handle();
    };
}   // namespace std

thread::detach

void detach();

The member function detaches the thead of execution, if any, associated with the calling object; thus, the operating system becomes responsible for releasing any resources owned by the thread when the thread terminates. After a call to detach(), a call to get_id() returns id().

If the thread associated with the calling object is not joinable, the function throws an object of type system_error with an error code of invalid_argument. If the thread associated with the calling object is invalid, the function throws an object of type system_error with an error code of no_such_process.

thread::get_id

id get_id() const noexcept;

The member function returns an object of type thread::id that uniquely identifies the thread of execution associated with this object, or thread::id() if there is no thread of execution associated with this object.

thread::hardware_concurrency

unsigned hardware_concurrency() noexcept;

The static member function returns an estimate of the number of hardware thread contexts. If this value cannot be computed or is not well defined it returns 0.

thread::id

struct id {
    id() noexcept;
    };

The struct thread::id provides a unique identifier for each thread of execution in the process. Its default constructor creates an object that does not compare equal to the thread::id object for any existing thread. All default-constructed thread::id objects compare equal.

thread::join

void join();

The member function blocks until the thread of execution associated with the calling object completes. If the call succeeds, subsequent calls to get_id() for the calling object return id(); if the call does not succeed, the value returned by get_id() is unchanged.

If the thread associated with the calling object is not joinable, the function throws an object of type system_error with an error code of invalid_argument. If the thread associated with the calling object is the current thread (i.e. get_id() == this_thread::get_id()) or if the system otherwise detects that a deadlock would occur, the function throws an object of type system_error with an error code of resource_deadlock_would_occur. If the thread associated with the calling object is invalid, the function throws an object of type system_error with an error code of no_such_process.

thread::joinable

bool joinable() const noexcept;

The member function returns true if the thread of execution associated with the calling object is joinable, otherwise false.

A thread object is joinable if get_id() != id().

thread::native_handle

native_handle_type native_handle();

The member function returns an object of type native_handle_type that can be used in implementation-specific ways.

thread::native_handle_type

typedef h-type native_handle_type;

The typedef is a synonym for an implementation-specific type that can be used in implementation-specific ways.

In this implementation, it is a synonym for thrd_t, and can be used as an argument to any of the thrd_XXX functions other than thrd_create.

thread::operator=

thread& operator=(thread&& Right) noexcept;

The member function calls detach() if the calling object is joinable. Regardless, it then associates the calling object with the thread associated with Right and sets Right to a default-constructed state.

thread::swap

void swap(thread& Other) noexcept;

The member function swaps the states of the calling object and Other.

thread::thread

thread() noexcept;
template<class Fn, class... Args>
    explicit thread(Fn&& F, Args&&... A);
thread(thread&& Other) noexcept;

The first constructor constructs an object that is not associated with any thread of execution. The value returned by a call to get_id() for such an object compares equal to thread::id().

The second constructor constructs an object that is associated with a new thread of execution and executes INVOKE(F, t1, t2, ..., tN) (where t1, t2, ..., tN are the values in args...) in the new thread. If there are not enough resources available to start a new thread, the function throws an object of type system_error with an error code of resource_unavailable_try_again. If the call to F terminates with an uncaught exception terminate() is called.

The third constructor constructs an object that is associated with the the thread associated with Other and sets Other to a default-constructed state.

thread::~thread

~thread();

The destructor calls terminate() if the thread object is joinable.

this_thread::yield

void yield() noexcept;

The function tells the operating system that it may run other threads even if the current thread would ordinarily continue to run.


See also the Table of Contents and the Index.

Copyright © 1992-2013 by P.J. Plauger. All rights reserved.