第1.1节 使用多线程场景
- 耗时操作,任务分解,实时响应.
- 充分利用多核CPU处理数据
- 读写分离、解耦合设计
第1.2节 使用多线程的简单案例
#include <iostream>
#include <thread>
using namespace std;
void ThreadMain()
{
cout << "begin sub thread main: " << this_thread::get_id() << endl;
for(int i = 0; i < 10; i++)
{
cout << "in thread " << i << endl;
this_thread::sleep_for(chrono::seconds(1)); // 1000ms
}
cout << "end sub thread main: " << this_thread::get_id() <<endl;
}
int main()
{
cout << "Main Id:" << this_thread::get_id() << endl;
// 线程创建启动
thread th(ThreadMain);
// 阻塞等待子线程退出
cout << "begin wait sub thread" << endl;
th.join();
cout << "end wait sub thread