
- The C Standard Library
- The C Standard Library
- The C++ Standard Library
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ Thread Library - Function constructor
Description
It is used to constructs a thread object.
Declaration
Following is the declaration for std::thread::thread function.
thread() noexcept; template <class Fn, class... Args> explicit thread (Fn&& fn, Args&&... args); thread (const thread&) = delete; thread (thread&& x) noexcept;
C++11
thread() noexcept; template <class Fn, class... Args> explicit thread (Fn&& fn, Args&&... args); thread (const thread&) = delete; thread (thread&& x) noexcept;
Parameters
fn − It is a pointer to function, pointer to member, or any kind of move-constructible function object.
args... − Arguments passed to the call to fn.
x − It is a thread object.
Return Value
none
Exceptions
none
Data races
modifies x.
Example
In below example explains about std::thread::thread function.
#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 << "1st Thread 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 << "2nd Thread executing\n"; ++n; std::this_thread::sleep_for(std::chrono::milliseconds(10)); } } int main() { int n = 0; std::thread t1; std::thread t2(f1, n + 1); std::thread t3(f2, std::ref(n)); std::thread t4(std::move(t3)); t2.join(); t4.join(); std::cout << "Final value of n is " << n << '\n'; }
Let us compile and run the above program, this will produce the following result −
1st Thread executing 2nd Thread executing 1st Thread executing 2nd Thread executing 1st Thread executing 2nd Thread executing 1st Thread executing 2nd Thread executing 2nd Thread executing 1st Thread executing Final value of n is 5
thread.htm
Advertisements