目录
- 启动线程
- 等待线程与分离线程
- 转移线程所有权
- 线程唯一标识符
- 例子
- 总结
参考资料:cppreference和c++并发编程实战
1. 启动线程
void do_some_work();
std::thread my_thread(do_some_work);
使用C++线程库启动线程,归结为构造std::thread对象, thread的构造函数。
- other: 另外一个线程对象
- f 可调用的对象
- args… 传递给函数对象的参数
1.1 函数指针+值传递
void f1(int n)
{
for(int i = 0; i < 5; i++){
cout<<"Thread1 executing"<<endl;
++n;
this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
int n = 0;
thread t1(f1, n);
1.2 函数指针+引用传递
void f2(int& n)
{
for(int i = 0; i < 5; i++){
cout<<"Thread2 executing"<<endl;
++n;
this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
int n = 0;
thread t2(f1, std::ref(n));
1.3 类成员函数
class foo
{
public:
void bar()
{
for (int i = 0; i < 5; ++i)
{
std::cout << "Thread 3 executing\n";
++n;
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
int n = 0;
};
thread t3(&foo::bar, &f);
1.4 重载运算符的类
class baz
{
public:
void operator()()
{
for (int i = 0; i < 5; ++i)
{
std::cout << "Thread 4 executing\n";
++n;
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
int n = 0;
};
baz b;
thread t4(b);
lambda表达式也可以作为构造函数的参数
2. 等待线程与分离线程
join阻塞等待调用线程运行结束,detach将主线程和子线程分离运行子线程独立的运行。
#include <iostream>
#include <thread>
#include <chrono>
void foo()
{
// simulate expensive operation
std::this_thread::sleep_for(std::chrono::seconds(1));
}
void bar()
{
// simulate expensive operation
std::this_thread::sleep_for(std::chrono::seconds(1));
}
int main()
{
std::cout << "starting first helper...\n";
std::thread helper1(foo);
std::cout << "starting second helper...\n";
std::thread helper2(bar);
std::cout << "waiting for helpers to finish..." << std::endl;
helper1.join();
helper2.join();
std::cout << "done!\n";
}
如果,主线程没有等待子线程结束就退出,可能会发生异常。因为,主线程退出将所有的资源都释放,如果线程函数还持有局部变量的指针或引用就会发生异常,这在单线程内访问已经释放的局部对象的指针或引用也是如此。
class Object{
public:
Object(int data = 0): data(data) {}
int data;
};
void func(Object* p)
{
this_thread::sleep_for(chrono::seconds(1)); // 模拟耗时的操作
cout<<"p->data: "<<p->data<<endl; // error
}
int main()
{
Object obj;
thread t(func, &obj); // 没有调用join
return 0;
}
子线程有一个Object
对象的指针,主线程没有等待子线程运行完毕就退出,当子线程访问该对象执行的内存时发生异常:terminate called without an active exception
。
3. 转移线程的所有权
thread支持移动构造,说明执行线程的所有权是可以在std::thread实例中移动的。c++标准库中有很多资源占用的类型,比如std::ifstream,std::unique_ptr还有std::thread都是可移动,但不可拷贝。
void some_function();
void some_other_function();
std::thread t1(some_function); // 1
std::thread t2=std::move(t1); // 2
t1=std::thread(some_other_function); // 3
std::thread t3; // 4
t3=std::move(t2); // 5
t1=std::move(t3); // 6 赋值操作将使程序崩溃
线程所有权可以转移,意味者线程可以作为函数返回值、可以作为参数进行传递。
void f(std::thread t);
void g()
{
void some_function();
f(std::thread(some_function)); // 1. 临时thread对象作为函数参数
std::thread t(some_function);
f(std::move(t)); // 2. 将std::move(t)将t转为右值
}
std::thread
可以作为参数的一个好处就是可以创建scoped_thread,类似std::lock_guard,利用RAII机制自动的对thread对象调用join,省去不必要的麻烦。
class scoped_thread
{
std::thread t;
public:
explicit scoped_thread(std::thread t_): // 1. 将t作为参数
t(std::move(t_))
{
if(!t.joinable()) // 2
throw std::logic_error(“No thread”);
}
~scoped_thread()
{
t.join(); // 3. 在析构函数里面调用join
}
scoped_thread(scoped_thread const&)=delete;
scoped_thread& operator=(scoped_thread const&)=delete;
};
scoped_thread t(func());
std::thread
还可以作为容器的元素,对于一个容器内的一组线程,可以统一调用join。将std::thread放入std::vector是向线程自动化管理迈出的第一步:并非为这些线程创建独立的变量,并且将他们直接加入,可以把它们当做一个组。创建一组线程(数量在运行时确定),而非创建固定数量的线程。
void do_work(unsigned id);
void f()
{
std::vector<std::thread> threads;
for(unsigned i=0; i < 20; ++i)
{
threads.push_back(std::thread(do_work,i)); // 产生线程
}
std::for_each(threads.begin(),threads.end(),
std::mem_fn(&std::thread::join)); // 对每个线程调用join()
}
4. 标识线程
每个线程都有一个标识,标识类型为std::thread::id
。可以通过两种方式进行检索。第一种,可以通过调用std::thread
对象的成员函数get_id()
来直接获取。如果std::thread
对象没有与任何执行线程相关联,get_id()
将返回std::thread::type
默认构造值,这个值表示“无线程”。第二种,当前线程中调用std::this_thread::get_id()
(这个函数定义在thread头文件中)也可以获得线程标识。
std::thread::id
实例常用作检测线程是否需要进行一些操作,比如:当用线程来分割一项工作主线程可能要做一些与其他线程不同的工作。这种情况下,启动其他线程前,它可以将自己的线程ID通过std::this_thread::get_id()
得到,并进行存储。
std::thread::id master_thread;
void some_core_part_of_algorithm()
{
if(std::this_thread::get_id()==master_thread) // 检查id
{
do_master_thread_work(); // 主线程的任务
}
do_common_work();
}
5. 例子
thread::hardware_concurrency()
返回能同时并发在一个程序中的线程数量,在多核系统中返回值一般是CPU核心的数量。下面这个例子调用thread::hardware_concurrency()
获得thread_num
为8,因为我的电脑是8核的,将10000000个数据的数组分割成thread_num
个部分,每个部分交给一个线程做计算,最后统计累计和,然后与单线程下做求和运算的运行时间作对比。
#include<thread>
#include<iostream>
#include<vector>
#include<time.h>
#include<numeric>
#include<chrono>
#include<algorithm>
using namespace std;
#define LENGTH 10000000
#define MAXNUM 100
void parallel_accumulate(vector<int>::iterator begin, vector<int>::iterator end, long& result)
{
result = accumulate(begin, end, 0);
}
int main()
{
srand(time(0));
// 准备数据
vector<int> nums(LENGTH, 0);
for(auto& n: nums)
{
n = rand() % MAXNUM;
}
unsigned int thread_num = thread::hardware_concurrency(); // CPU数量
cout<<"thread num: "<<thread_num<<endl;
vector<long> results(thread_num, 0); // 每个线程的保存结果
vector<thread> threads; // 线程组
int block_size = LENGTH / thread_num; // 分块大小
auto block_begin = nums.begin(), block_end = block_begin;
auto before1 = chrono::steady_clock::now(); // 计时
for(unsigned int i = 0; i < thread_num; i++)
{
if(i == thread_num - 1){
block_end = nums.end();
}else{
advance(block_end, block_size);
}
threads.emplace_back(parallel_accumulate, block_begin, block_end, std::ref(results[i]));
block_begin = block_end;
}
for(auto& t: threads)
{
t.join();
}
long sum1 = accumulate(results.begin(), results.end(), 0); // 所有线程的累计结果
auto after1 = chrono::steady_clock::now();
double cost1 = chrono::duration<double, std::micro>(after1 - before1).count(); // 多线程运行时间 单位微秒
auto before2 = chrono::steady_clock::now(); // 计时
long sum2 = accumulate(nums.begin(), nums.end(), 0); // 单线程累加
auto after2 = chrono::steady_clock::now();
double cost2 = chrono::duration<double, std::micro>(after2 - before2).count(); // 单线程运行时间 单位微秒
cout<<"multi thread result: "<<sum1<<" cost time: "<<cost1<<endl;
cout<<"single thread result: "<<sum2<<" cost time: "<<cost2<<endl;
cout<<"multi thread / single thread: "<<cost1/cost2<<endl;
return 0;
}
最后的实验结果为,可以看出将数据分为thread num块并行计算的时间开销约为单线程的28%,并不一定是1/thread num,因为创建线程、线程切换也有一定开销。
thread num: 8
multi thread result: 494438377 cost time: 20511.9
single thread result: 494438377 cost time: 73372.7
multi thread / single thread: 0.279558
6. 总结
- 线程的启动方式。构造函数的传参包括:函数指针、函数对象、重载()运算符的类还有lambda表达式等,只要是可调用的对象都可以作为线程的参数。然后传引用需要
std::ref
修饰,否则为传值。 - 线程join和detach。
- 线程的所有权转接。线程的所有权转接也就是移动构造和移动赋值,可以将thread对象作为函数的参数进行传递或函数的返回值,然后举例scoped_thread 说明了其作用。
- 标识线程。可以通过调用
std::thread
对象的成员函数get_id()
和std::this_thread::get_id()
获取线程标识符,线程标识符可以区分不同的线程。 - 最后,给出了一个数据并行计算的例子,将数据分割在不同核心上计算,相比于单线程极大的缩小了计算时间。