thread — Klasathread Class
Definiuje obiekt, który jest używany do obserwowania i zarządzania wątkiem wykonywania w obrębie aplikacji.Defines an object that's used to observe and manage a thread of execution within an application.
SkładniaSyntax
class thread;
UwagiRemarks
Można użyć thread
obiektu do obserwowania wątku wykonywania w aplikacji i zarządzania nim.You can use a thread
object to observe and manage a thread of execution within an application. Obiekt wątku, który jest tworzony przy użyciu konstruktora domyślnego nie jest skojarzony z żadnym wątkiem wykonywania.A thread object that's created by using the default constructor is not associated with any thread of execution. Obiekt wątku, który jest konstruowany przy użyciu wywoływanego obiektu tworzy nowy wątek wykonywania i wywołuje obiekt w tym wątku.A thread object that's constructed by using a callable object creates a new thread of execution and calls the callable object in that thread. Obiekty wątków można przenosić, ale nie kopiować.Thread objects can be moved but not copied. W związku z tym wątek wykonywania może być skojarzony tylko z jednym obiektem wątku.Therefore, a thread of execution can be associated with only one thread object.
Każdy wątek wykonywania ma unikatowy identyfikator typu thread::id
.Every thread of execution has a unique identifier of type thread::id
. Funkcja this_thread::get_id
zwraca identyfikator wywołującego wątku.The function this_thread::get_id
returns the identifier of the calling thread. Element członkowski funkcji thread::get_id
zwraca identyfikator wątku, który jest zarządzany przez obiekt wątku.The member function thread::get_id
returns the identifier of the thread that's managed by a thread object. Dla obiektu wątku zbudowanego domyślnie, metoda thread::get_id
zwraca obiekt o wartości, która jest taka sama dla wszystkich obiektów wątku zbudowanego domyślnie i różni się od wartości zwracanej przez this_thread::get_id
dla każdego wątku wykonywania, która może być połączona w momencie wywołania.For a default-constructed thread object, the thread::get_id
method returns an object that has a value that's the same for all default-constructed thread objects and different from the value that's returned by this_thread::get_id
for any thread of execution that could be joined at the time of the call.
Elementy członkowskieMembers
Klasy publicznePublic Classes
NazwaName | OpisDescription |
---|---|
Thread:: ID — Klasathread::id Class | Jednoznacznie identyfikuje skojarzony wątek.Uniquely identifies the associated thread. |
Konstruktory publicznePublic Constructors
NazwaName | OpisDescription |
---|---|
wątekthread | Konstruuje thread obiekt.Constructs a thread object. |
Metody publicznePublic Methods
NazwaName | OpisDescription |
---|---|
ekrandetach | Odłącza skojarzony wątek od thread obiektu.Detaches the associated thread from the thread object. |
get_idget_id | Zwraca unikatowy identyfikator skojarzonego wątku.Returns the unique identifier of the associated thread. |
hardware_concurrencyhardware_concurrency | Statyczny.Static. Zwraca szacunkową liczbę kontekstów wątków sprzętu.Returns an estimate of the number of hardware thread contexts. |
Złączajoin | Blokuje, aż do zakończenia skojarzonego wątku.Blocks until the associated thread completes. |
sprzęganiujoinable | Określa, czy skojarzony wątek podlega sprzęganiu.Specifies whether the associated thread is joinable. |
native_handlenative_handle | Zwraca typ zależny od implementacji, który reprezentuje dojście wątku.Returns the implementation-specific type that represents the thread handle. |
wymianyswap | Zamienia stan obiektu na określony thread obiekt.Swaps the object state with a specified thread object. |
Operatory publicznePublic Operators
NazwaName | OpisDescription |
---|---|
Thread:: operator =thread::operator= | Kojarzy wątek z bieżącym thread obiektem.Associates a thread with the current thread object. |
WymaganiaRequirements
Nagłówek:<thread>Header: <thread>
Przestrzeń nazw: stdNamespace: std
Wątek::d etachthread::detach
Odłącza skojarzony wątek.Detaches the associated thread. System operacyjny jest odpowiedzialny za zwolnienie zasobów wątku po zakończeniu.The operating system becomes responsible for releasing thread resources on termination.
void detach();
UwagiRemarks
Po wywołaniu detach
, kolejne wywołania do get_id Identyfikatorpowrotu.After a call to detach
, subsequent calls to get_id return id.
Jeśli wątek, który jest skojarzony z obiektem wywołującym nie jest przyłączany, funkcja zgłasza system_error , który ma kod błędu invalid_argument
.If the thread that's associated with the calling object is not joinable, the function throws a system_error that has an error code of invalid_argument
.
Jeśli wątek skojarzony z obiektem wywołującym jest nieprawidłowy, funkcja zgłasza, system_error
że ma kod błędu no_such_process
.If the thread that's associated with the calling object is invalid, the function throws a system_error
that has an error code of no_such_process
.
Wątek:: get_idthread::get_id
Zwraca unikatowy identyfikator skojarzonego wątku.Returns a unique identifier for the associated thread.
id get_id() const noexcept;
Wartość zwracanaReturn Value
Wątek:: ID , który jednoznacznie identyfikuje skojarzony wątek, lub thread::id()
Jeśli żaden wątek nie jest skojarzony z obiektem.A thread::id object that uniquely identifies the associated thread, or thread::id()
if no thread is associated with the object.
Wątek:: hardware_concurrencythread::hardware_concurrency
Metoda statyczna zwracająca szacunkową liczbę kontekstów wątków sprzętowych.Static method that returns an estimate of the number of hardware thread contexts.
static unsigned int hardware_concurrency() noexcept;
Wartość zwracanaReturn Value
Oszacowanie liczby kontekstów wątków sprzętowych.An estimate of the number of hardware thread contexts. Jeśli wartość nie może być obliczona lub nie jest prawidłowo zdefiniowana, ta metoda zwraca 0.If the value cannot be computed or is not well defined, this method returns 0.
Thread:: ID — Klasathread::id Class
Zapewnia unikatowy identyfikator dla każdego wątku wykonywania w procesie.Provides a unique identifier for each thread of execution in the process.
class thread::id {
id() noexcept;
};
UwagiRemarks
Konstruktor domyślny tworzy obiekt, który nie jest porównywany z thread::id
obiektem w żadnym istniejącym wątku.The default constructor creates an object that does not compare equal to the thread::id
object for any existing thread.
Wszystkie obiekty skonstruowane domyślnie są thread::id
równe.All default-constructed thread::id
objects compare equal.
Wątek:: jointhread::join
Bloki do momentu, aż wątek wykonywania skojarzony z wywoływanym obiektem zostanie zakończony.Blocks until the thread of execution that's associated with the calling object completes.
void join();
UwagiRemarks
Jeśli wywołanie powiedzie się, kolejne wywołania get_id dla obiektu wywołującego zwracają domyślny wątek:: ID , który nie jest porównywany thread::id
z żadnym istniejącym wątkiem. Jeśli wywołanie nie powiedzie się, wartość zwracana przez get_id
jest niezmieniona.If the call succeeds, subsequent calls to get_id for the calling object return a default thread::id that does not compare equal to the thread::id
of any existing thread; if the call does not succeed, the value that's returned by get_id
is unchanged.
Wątek:: jointhread::joinable
Określa, czy skojarzony wątek jest przyłączony.Specifies whether the associated thread is joinable.
bool joinable() const noexcept;
Wartość zwracanaReturn Value
true
Jeśli skojarzony wątek jest przyłączany; w przeciwnym razie false
.true
if the associated thread is joinable; otherwise, false
.
UwagiRemarks
Obiekt wątku jest przyłączany , jeśli get_id() != id()
.A thread object is joinable if get_id() != id()
.
Wątek:: native_handlethread::native_handle
Zwraca typ zależny od implementacji, który reprezentuje dojście wątku.Returns the implementation-specific type that represents the thread handle. Uchwyt wątku może być używany w sposób specyficzny dla implementacji.The thread handle can be used in implementation-specific ways.
native_handle_type native_handle();
Wartość zwracanaReturn Value
native_handle_type
jest definiowana jako system Win32 HANDLE
, który jest rzutowany jako void *
.native_handle_type
is defined as a Win32 HANDLE
that's cast as void *
.
Thread:: operator =thread::operator=
Kojarzy wątek określonego obiektu z bieżącym obiektem.Associates the thread of a specified object with the current object.
thread& operator=(thread&& Other) noexcept;
ParametryParameters
RóżnychOther
Obiekt thread
.A thread
object.
Wartość zwracanaReturn Value
*this
UwagiRemarks
Wywołania metody odłączają się, gdy wywołujący obiekt jest przyłączany.The method calls detach if the calling object is joinable.
Po utworzeniu skojarzenia Other
jest ustawiany domyślny stan skonstruowany.After the association is made, Other
is set to a default-constructed state.
Wątek:: swapthread::swap
Zamienia stan obiektu na określony thread
obiekt.Swaps the object state with that of a specified thread
object.
void swap(thread& Other) noexcept;
ParametryParameters
RóżnychOther
Obiekt thread
.A thread
object.
Thread:: Thread — Konstruktorthread::thread Constructor
Konstruuje thread
obiekt.Constructs a thread
object.
thread() noexcept;
template <class Fn, class... Args>
explicit thread(Fn&& F, Args&&... A);
thread(thread&& Other) noexcept;
ParametryParameters
NF
Funkcja zdefiniowana przez aplikację do wykonania przez wątek.An application-defined function to be executed by the thread.
ZA
Lista argumentów do przesłania do F.A list of arguments to be passed to F.
RóżnychOther
Istniejący thread
obiekt.An existing thread
object.
UwagiRemarks
Pierwszy Konstruktor konstruuje obiekt, który nie jest skojarzony z wątkiem wykonania.The first constructor constructs an object that's not associated with a thread of execution. Wartość zwracana przez wywołanie get_id
dla konstruowanego obiektu to thread::id()
.The value that's returned by a call to get_id
for the constructed object is thread::id()
.
Drugi Konstruktor konstruuje obiekt, który jest skojarzony z nowym wątkiem wykonywania i wykonuje pseudo funkcja INVOKE
, która jest zdefiniowana w <functional> .The second constructor constructs an object that's associated with a new thread of execution and executes the pseudo-function INVOKE
that's defined in <functional>. Jeśli za mało zasobów jest dostępnych do uruchomienia nowego wątku, funkcja zgłasza obiekt system_error , który ma kod błędu resource_unavailable_try_again
.If not enough resources are available to start a new thread, the function throws a system_error object that has an error code of resource_unavailable_try_again
. Jeśli wywołanie F kończy się nieprzechwyconym wyjątkem, zostanie wywołane zakończenie .If the call to F terminates with an uncaught exception, terminate is called.
Trzeci Konstruktor konstruuje obiekt, który jest skojarzony z wątkiem, który jest skojarzony z Other
.The third constructor constructs an object that's associated with the thread that's associated with Other
. Other
jest następnie ustawiany na stan skonstruowany domyślnie.Other
is then set to a default-constructed state.
Zobacz teżSee also
Dokumentacja plików nagłówkowychHeader Files Reference
<thread>