std::thread::thread
Aus cppreference.com
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
thread(); |
(1) | (seit C++11) |
thread( thread&& other ); |
(2) | (seit C++11) |
template< class Function, class... Args > explicit thread( Function&& f, Args&&... args ); |
(3) | (seit C++11) |
thread(const thread&) = delete; |
(4) | (seit C++11) |
Erzeugt neue Thread-Objekt .
Original:
Constructs new thread object.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
1)
Schafft neue Thread-Objekt, das es sich nicht um Thread .
Original:
Creates new thread object which does not represent a thread.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
2)
Bewegen Konstruktor. Erzeugt das Thread-Objekt, um die Ausführungs-Threads, die von
other vertreten war vertreten. Nach diesem Aufruf other nicht mehr ein Ausführungs-Thread .Original:
Move constructor. Constructs the thread object to represent the thread of execution that was represented by
other. After this call other no longer represents a thread of execution.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
3)
Schafft neue
std::thread Objekt und verbindet es mit einem Thread der Ausführung. Zuerst der Konstruktor kopiert alle Argumente args... um lokalen Thread-Speicher, wie von der Funktion:Original:
Creates new
std::thread object and associates it with a thread of execution. First the constructor copies all arguments args... to thread-local storage as if by the function:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
template <class T>
typename decay<T>::type decay_copy(T&& v) {
return std::forward<T>(v);
}
@ @ Ausnahmen bei der Auswertung und Kopieren der Argumente geworfen werden in den aktuellen Thread, nicht der neue Thread geworfen .
Original:
@@ Any exceptions thrown during evaluation and copying of the arguments are thrown in the current thread, not the new thread.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
@ @ Der Code, der in der neuen Thread ausgeführt werden wie folgt definiert. Lassen Sie beziehen sich auf None
copied_args als t1, t2, ..., tN, wo N ist sizeof...(copied_args)and copied_args is the result of calling decay_copy as defined above. The following code will be run in the new thread: - Wenn
fZeiger auf eine Elementfunktion der KlasseTist, dann heißt es. Der Rückgabewert wird ignoriert. Effektiv wird der folgende Code ausgeführt:Original:Iffis pointer to a member function of classT, then it is called. The return value is ignored. Effectively, the following code is executed:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
(t1.*f)(t2, ..., tN)wenn der Typ dest1entwederT, anhandToder Referenz zu Typ abgeleitet vonT.Original:(t1.*f)(t2, ..., tN)if the type oft1is eitherT, reference toTor reference to type derived fromT.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.((*t1).*f)(t2, ..., tN)sonst .Original:((*t1).*f)(t2, ..., tN)otherwise.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- Wenn
N == 1undfist Zeiger auf ein Element Daten einer Klasse, dann wird es abgerufen. Der Wert des Objekts wird ignoriert. Effektiv wird der folgende Code ausgeführt:Original:IfN == 1andfis pointer to a member data object of a class, then it is accessed. The value of the object is ignored. Effectively, the following code is executed:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
t1.*fwenn und die Art dert1entwederT, anhandToder Referenz zu Typ abgeleitet vonT.Original:t1.*fif and the type oft1is eitherT, reference toTor reference to type derived fromT.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.(*t1).*fsonst .Original:(*t1).*fotherwise.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
fwird als Zeiger auf eine nicht Memberfunktion in allen anderen Fällen bezeichnet. Der Rückgabewert wird ignoriert. Effektiv wirdf(t1, t2, ..., tN)ausgeführt .Original:fis called as a pointer to a non-member function in all other cases. The return value is ignored. Effectively,f(t1, t2, ..., tN)is executed.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Original:
@@ The code that will be run in the new thread is defined as follows. Lassen Sie beziehen sich auf None
copied_args als t1, t2, ..., tN, wo N ist sizeof...(copied_args)and copied_args is the result of calling decay_copy as defined above. The following code will be run in the new thread: - Wenn
fZeiger auf eine Elementfunktion der KlasseTist, dann heißt es. Der Rückgabewert wird ignoriert. Effektiv wird der folgende Code ausgeführt:Original:Iffis pointer to a member function of classT, then it is called. The return value is ignored. Effectively, the following code is executed:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
(t1.*f)(t2, ..., tN)wenn der Typ dest1entwederT, anhandToder Referenz zu Typ abgeleitet vonT.Original:(t1.*f)(t2, ..., tN)if the type oft1is eitherT, reference toTor reference to type derived fromT.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.((*t1).*f)(t2, ..., tN)sonst .Original:((*t1).*f)(t2, ..., tN)otherwise.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- Wenn
N == 1undfist Zeiger auf ein Element Daten einer Klasse, dann wird es abgerufen. Der Wert des Objekts wird ignoriert. Effektiv wird der folgende Code ausgeführt:Original:IfN == 1andfis pointer to a member data object of a class, then it is accessed. The value of the object is ignored. Effectively, the following code is executed:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
t1.*fwenn und die Art dert1entwederT, anhandToder Referenz zu Typ abgeleitet vonT.Original:t1.*fif and the type oft1is eitherT, reference toTor reference to type derived fromT.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.(*t1).*fsonst .Original:(*t1).*fotherwise.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
fwird als Zeiger auf eine nicht Memberfunktion in allen anderen Fällen bezeichnet. Der Rückgabewert wird ignoriert. Effektiv wirdf(t1, t2, ..., tN)ausgeführt .Original:fis called as a pointer to a non-member function in all other cases. The return value is ignored. Effectively,f(t1, t2, ..., tN)is executed.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
4)
Der Copy-Konstruktor wird gestrichen Fäden sind nicht kopierbar. Keine zwei
std::thread Objekte können auf den gleichen Thread der Ausführung .Original:
The copy constructor is deleted; threads are not copyable. No two
std::thread objects may represent the same thread of execution.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Parameter
| other | - | ein weiterer Thread-Objekt, um dieses Thread-Objekt mit zu bauen
Original: another thread object to construct this thread object with The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
| f | - | funktionieren im neuen Thread ausgeführt werden
Original: function to execute in the new thread The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
| args... | - | Argumente, die an die neue Funktion zu übergeben
Original: arguments to pass to the new function The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
Ausnahmen
1-2)
3)
std::system_error wenn der Thread nicht gestartet werden konnte. Die Ausnahme darstellen können den Fehler
std::errc::resource_unavailable_try_again oder eine andere Implementierung spezifischer Fehlerbedingung .Original:
std::system_error if the thread could not be started. The exception may represent the error condition
std::errc::resource_unavailable_try_again or another implementation-specific error condition.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Notes
Die Argumente für die Thread-Funktion werden durch Wert kopiert. Wenn ein Verweis Argument muss der Thread-Funktion übergeben werden, muss es gewickelt werden (z. B. mit std::ref oder std::cref) .
Original:
The arguments to the thread function are copied by value. If a reference argument needs to be passed to the thread function, it has to be wrapped (e.g. with std::ref or std::cref).
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Jede Rückgabewert der Funktion wird ignoriert. Wenn die Funktion eine Ausnahme auslöst, wird std::terminate genannt. Um Rückgabewerte oder Ausnahmen zurück an den aufrufenden Thread, std::promise oder std::async verwendet werden .
Original:
Any return value from the function is ignored. If the function throws an exception, std::terminate is called. In order to pass return values or exceptions back to the calling thread, std::promise or std::async may be used.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Beispiel
#include <iostream>
#include <utility>
#include <thread>
#include <chrono>
#include <functional>
#include <atomic>
void f1(int n)
{
for(int i=0; i<5; ++i) {
std::cout << "Thread " << n << " executing\n";
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
void f2(int& n)
{
for(int i=0; i<5; ++i) {
std::cout << "Thread 2 executing\n";
++n;
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
int main()
{
int n = 0;
std::thread t1; // t1 is not a thread
std::thread t2(f1, n+1); // pass by value
std::thread t3(f2, std::ref(n)); // pass by reference
std::thread t4(std::move(t3)); // t4 is now running f2(). t3 is no longer a thread
t2.join();
t4.join();
std::cout << "Final value of n is " << n << '\n';
}
Possible output:
Thread 1 executing
Thread 2 executing
Thread 1 executing
Thread 2 executing
Thread 1 executing
Thread 2 executing
Thread 1 executing
Thread 2 executing
Thread 1 executing
Thread 2 executing
Final value of n is 5