std::thread
C++ Thread support library std::thread. Threads enable programs to execute across several processor cores.
Defined in header
Class :
thread
Member types :
native_handle_type
Data Member
id //represents the id of a thread (public member class)
Member functions
(constructor) //constructs new thread object (public member function)
(destructor) //destructs the thread object, underlying thread must be joined or detached (public member function)
operator= //moves the thread object
Constructor :
Destructor:
std::thread::operator= :
C++ Thread support library std::thread
thread& operator=( thread&& other ) noexcept;
(since C++11)
If *this still has an associated running thread (i.e. joinable() == true), call std::terminate(). Otherwise, assigns the state of other to *this and sets other to a default constructed state.
After this call, this->get_id() is equal to the value of other.get_id() prior to the call, and other no longer represents a thread of execution.
other - another thread object to assign to this thread object
*this
C++ Thread support library std::thread. Threads enable programs to execute across several processor cores.
Defined in header
Class :
thread
Member types :
native_handle_type
Data Member
id //represents the id of a thread (public member class)
Member functions
(constructor) //constructs new thread object (public member function)
(destructor) //destructs the thread object, underlying thread must be joined or detached (public member function)
operator= //moves the thread object
Observers // Covered in NEXT POST
Operations // Covered in NEXT POST
Constructor :
#includevoid f1(int n) { for (int i = 0; i < 5; ++i) { std::cout << "Thread 1 executing\n"; ++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'; }
DON't WORRY ABOUT OUTPUT AT THIS STAGE
Destructor:
~thread();
| (since C++11) | |
Destroys the thread object.
std::thread::operator= :
C++ Thread support library std::thread
thread& operator=( thread&& other ) noexcept;
(since C++11)
If *this still has an associated running thread (i.e. joinable() == true), call std::terminate(). Otherwise, assigns the state of other to *this and sets other to a default constructed state.
After this call, this->get_id() is equal to the value of other.get_id() prior to the call, and other no longer represents a thread of execution.
- Parameters :
other - another thread object to assign to this thread object
- Return value :
*this
Comments
Post a Comment