1.概述
1.1.与 C++11 多线程相关的头文件
<atomic>:该头文主要声明了两个类, std::atomic 和 std::atomic_flag,另外还声明了一套 C 风格的原子类型和与 C 兼容的原子操作的函数。
<thread>:该头文件主要声明了 std::thread 类,另外 std::this_thread 命名空间也在该头文件中。
<mutex>:该头文件主要声明了与互斥量(mutex)相关的类,包括 std::mutex 系列类,std::lock_guard, std::unique_lock, 以及其他的类型和函数。
<condition_variable>:该头文件主要声明了与条件变量相关的类,包括 std::condition_variable 和 std::condition_variable_any。
<future>:该头文件主要声明了 std::promise, std::package_task 两个 Provider 类,以及 std::future 和 std::shared_future
两个 Future 类,另外还有一些与之相关的类型和函数,std::async() 函数就声明在此头文件中。
1.2.std::thread 构造
(1)默认构造函数,创建一个空的 thread 执行对象。
thread() noexcept;
(2)初始化构造函数,创建一个 thread对象,该 thread对象可被 joinable,新产生的线程会调用 fn 函数,该函数的参数由 args 给出。
template <class Fn, class... Args> explicit thread (Fn&& fn, Args&&... args);
(3)拷贝构造函数(被禁用),意味着 thread 不可被拷贝构造。
thread& operator= thread (const thread&) = delete;
(4)move 构造函数,move 构造函数,调用成功之后 x 不代表任何 thread 执行对象,即线程x被移动到当前线程中,原线程x被销毁。
thread& operator= thread (thread&& x) noexcept;
注意:可被 joinable 的 thread 对象必须在他们销毁之前被主线程 join 或者将其设置为 detached.
1.3.其他成员函数
get_id 获取线程 ID。
joinable 检查线程是否可被 join。
join 将子线程加入主线程中,由主线程负责子线程的释放,这样做,在子线程执行时,主线程会堵塞住。
detach 分离子线程,此时主线程和子线程是2个独立的线程,子线程自己负责自己的释放。若是子线程抛出错误,会导致主线程崩溃。
swap Swap 线程 。
native_handle 返回 native handle。
hardware_concurrency [static] 检测硬件并发特性。
2.实例
2.1.最简单的多线程列子
#include <stdio.h>
#include <stdlib.h>
#include <iostream> // std::cout
#include <thread> // std::thread
void thread_task() {
std::cout << "hello thread" << std::endl;
}
int main(int argc, const char *argv[]){
std::thread t(thread_task);
t.join();
return EXIT_SUCCESS;
}
2.2.创建多线程列子
#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 (t1只是被声明为线程,但还没有初始化)
std::thread t2(f1, n + 1); // pass by value (t2是一条完整的线程,其在后面被join到主线程中,然后执行f1函数,当执行完了,被主线程销毁)
std::thread t3(f2, std::ref(n)); // pass by reference (t3是一条以std::ref方式传递参数的线程)
std::thread t4(std::move(t3)); // t4 is now running f2(). t3 is no longer a thread
t2.join(); //以阻塞式连接到主线程(t2执行时,主线程会被卡住,直到t2执行完成)
t4.join();
std::cout << "Final value of n is " << n << '\n';
}
2.3.线程的赋值操作列子
#include <stdio.h>
#include <stdlib.h>
#include <chrono> // std::chrono::seconds (std::chrono是一个时间模板库)
#include <iostream> // std::cout
#include <thread> // std::thread, std::this_thread::sleep_for
void thread_task(int n) {
std::this_thread::sleep_for(std::chrono::seconds(n));
std::cout << "hello thread "
<< std::this_thread::get_id()
<< " paused " << n << " seconds" << std::endl;
}
int main(int argc, const char *argv[]){
std::thread threads[5];
std::cout << "Spawning 5 threads...\n";
for (int i = 0; i < 5; i++)
threads[i] = std::thread(thread_task, i + 1); //这里就是线程赋值
std::cout << "Done spawning threads! Now wait for them to join\n";
for (auto& t: threads)
t.join();
std::cout << "All threads joined.\n";
return EXIT_SUCCESS;
}
1.1.与 C++11 多线程相关的头文件
<atomic>:该头文主要声明了两个类, std::atomic 和 std::atomic_flag,另外还声明了一套 C 风格的原子类型和与 C 兼容的原子操作的函数。
<thread>:该头文件主要声明了 std::thread 类,另外 std::this_thread 命名空间也在该头文件中。
<mutex>:该头文件主要声明了与互斥量(mutex)相关的类,包括 std::mutex 系列类,std::lock_guard, std::unique_lock, 以及其他的类型和函数。
<condition_variable>:该头文件主要声明了与条件变量相关的类,包括 std::condition_variable 和 std::condition_variable_any。
<future>:该头文件主要声明了 std::promise, std::package_task 两个 Provider 类,以及 std::future 和 std::shared_future
两个 Future 类,另外还有一些与之相关的类型和函数,std::async() 函数就声明在此头文件中。
1.2.std::thread 构造
(1)默认构造函数,创建一个空的 thread 执行对象。
thread() noexcept;
(2)初始化构造函数,创建一个 thread对象,该 thread对象可被 joinable,新产生的线程会调用 fn 函数,该函数的参数由 args 给出。
template <class Fn, class... Args> explicit thread (Fn&& fn, Args&&... args);
(3)拷贝构造函数(被禁用),意味着 thread 不可被拷贝构造。
thread& operator= thread (const thread&) = delete;
(4)move 构造函数,move 构造函数,调用成功之后 x 不代表任何 thread 执行对象,即线程x被移动到当前线程中,原线程x被销毁。
thread& operator= thread (thread&& x) noexcept;
注意:可被 joinable 的 thread 对象必须在他们销毁之前被主线程 join 或者将其设置为 detached.
1.3.其他成员函数
get_id 获取线程 ID。
joinable 检查线程是否可被 join。
join 将子线程加入主线程中,由主线程负责子线程的释放,这样做,在子线程执行时,主线程会堵塞住。
detach 分离子线程,此时主线程和子线程是2个独立的线程,子线程自己负责自己的释放。若是子线程抛出错误,会导致主线程崩溃。
swap Swap 线程 。
native_handle 返回 native handle。
hardware_concurrency [static] 检测硬件并发特性。
2.实例
2.1.最简单的多线程列子
#include <stdio.h>
#include <stdlib.h>
#include <iostream> // std::cout
#include <thread> // std::thread
void thread_task() {
std::cout << "hello thread" << std::endl;
}
int main(int argc, const char *argv[]){
std::thread t(thread_task);
t.join();
return EXIT_SUCCESS;
}
2.2.创建多线程列子
#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 (t1只是被声明为线程,但还没有初始化)
std::thread t2(f1, n + 1); // pass by value (t2是一条完整的线程,其在后面被join到主线程中,然后执行f1函数,当执行完了,被主线程销毁)
std::thread t3(f2, std::ref(n)); // pass by reference (t3是一条以std::ref方式传递参数的线程)
std::thread t4(std::move(t3)); // t4 is now running f2(). t3 is no longer a thread
t2.join(); //以阻塞式连接到主线程(t2执行时,主线程会被卡住,直到t2执行完成)
t4.join();
std::cout << "Final value of n is " << n << '\n';
}
2.3.线程的赋值操作列子
#include <stdio.h>
#include <stdlib.h>
#include <chrono> // std::chrono::seconds (std::chrono是一个时间模板库)
#include <iostream> // std::cout
#include <thread> // std::thread, std::this_thread::sleep_for
void thread_task(int n) {
std::this_thread::sleep_for(std::chrono::seconds(n));
std::cout << "hello thread "
<< std::this_thread::get_id()
<< " paused " << n << " seconds" << std::endl;
}
int main(int argc, const char *argv[]){
std::thread threads[5];
std::cout << "Spawning 5 threads...\n";
for (int i = 0; i < 5; i++)
threads[i] = std::thread(thread_task, i + 1); //这里就是线程赋值
std::cout << "Done spawning threads! Now wait for them to join\n";
for (auto& t: threads)
t.join();
std::cout << "All threads joined.\n";
return EXIT_SUCCESS;
}