6_2_创建线程.cpp
#include "hjcommon.hpp"
#include <thread> // c++98, 记得链接pthread库
using namespace std;
HJ_NS_USING
static void threadFunc2()
{
cout << "threadFunc2." << endl;
}
static void threadFunc1()
{
cout << "threadFunc1 start." << endl;
thread t(threadFunc2); // 子线程中创建子线程,get
t.join();
cout << "threadFunc1 end." << endl;
}
class A
{
public:
A() { cout << "no arg constructor." << endl; }
A(const A &a) { cout << "copy constructor." << endl; }
~A() { cout << "destructor." << endl; }
void operator()() { cout << "memeberFunc." << endl; }
};
int main_2_6_2(int argc, char *argv[])
{
// 线程创建的时候就会去执行,不需要是否 join 或 detach
// 正常方式创建thread
std::thread t1(threadFunc1); // thread()接受一个可调用对象
if (t1.joinable()) // joinable() 当程序调用过一次join()或detach()后返回false,否则返回true。 join()、detach()函数不能多次调用,不能两者同时调用
cout << "joinable true1." << endl;
else
cout << "joinable false1." << endl;
t1.join(); // 阻塞
// t1.detach(); // 分离,当主线程return后,理论上系统会关闭此程序进程,进程中其他未执行完的子线程也会被强行关闭。 不建议使用detach方式
if (t1.joinable()) cout << "joinable true2." << endl;
else cout << "joinable false2." << endl;
// 类的可调用对象创建thread
A a;
Thread t2(a);// 类的可调用对象创建thread,注意:线程中会复制一份对象a (ubuntu18中测试是复制了两份。。若线程detach,线程函数可能会执行两次。若线程join,只会执行一次)
t2.join();
// t2.detach();
// lambda表达式创建thread
auto lambda = []{
cout << "lambdaFunc." << endl;
};
Thread t3(lambda);
t3.join();
// t3.detach();
cout << "end." << endl;
return 0;
}
6_3_线程传参.cpp
#include "hjcommon.hpp"
#include <thread>
#include <memory>
using namespace std;
HJ_NS_USING
class A
{
public:
mutable int m_num; // thread函数中a定义为了const,所以需要mutable修饰,让修改
A(int num) : m_num(num)
{
cout << "one arg, this=" << hex << this << dec << ", threadId=" << std::this_thread::get_id() << endl;
}
A(const A &a) : m_num(a.m_num)
{
cout << "copy, this=" << hex << this << dec << ", threadId=" << std::this_thread::get_id() << endl;
}
~A()
{
cout << "destructor, this=" << hex << this << dec << ", threadId=" << std::this_thread::get_id() << endl;
}
void threadFunc(int num) { cout << "a threadFunc." << endl; }
void operator()(int num) { cout << "a operator()." << endl; }
};
static void aFunc(const int i, const A &a) // const &a 换成 const &&a 构造与析沟行为还是一样的
{
cout << "A : " << i << ", " << a.m_num << ", threadId=" << std::this_thread::get_id() << endl;
}
static void bFunc(const A &a)
{
a.m_num = 999;
cout << "B : " << a.m_num << ", threadId=" << std::this_thread::get_id() << endl;
}
static void cFunc(int &num)
{
num = 777;
cout << "C : " << num << ", threadId=" << std::this_thread::get_id() << endl;
}
static void dFunc(unique_ptr<A> pu, shared_ptr<A> ps)
{
cout << "thread d." << endl;
}
int main_2_6_3(int argc, char *argv[])
{
cout << "----------------隐式类型转换形式的临时对象给thread函数传参 与 直接构造形式的临时对象给thread函数传参----------------" << endl;
// thread函数参数可以无参,也可以是多个不同类型参数。 基础数据类型的形参不要定义为引用和指针。类类型的形参建议定义为引用(需要是常引用),不要定义为对象和对象指针
// 问题1:detach时,如果传栈区变量给thread 可能会存在主线程退出了,然后再将栈区变量 转换成thread函数形参 的可能性,这样程序会有不可预测后果。建议使用join
// 线程传参由于涉及到数据一致性、内存生命周期等问题,所以thread函数参数不管是引用类型还是非引用类型,编译器都会拷贝一份相同的实参。 所以问题1应该是一个是虚假命题
// 线程id : 每个线程都有一个各不相同的id thread::id std::this_thread::get_id() ,thread::id有一个 unsigned long int 成员,表示线程id
cout << "主线程 threadId=" << std::this_thread::get_id() << endl;
int num = 0; // 若detach形式,主线程执行完后num会被回收,注意此时对thread函数传参的影响
// 隐式类型转换形式的临时对象给thread函数传参 与 直接构造形式的临时对象给thread函数传参
Thread t2(aFunc, 2, num); // 隐式类型转换形式的,构造的临时对象是在子线程中构造,子线程中析沟(不同编译器构造的临时个数不同。不一定会发生拷贝构造)
t2.join(); // 安全
// t2.detach(); // 不安全
// 直接构造形式的,构造的临时对象是在主线程中构造,子线程中析沟。。(不同编译器构造的临时个数不同,但是A(num)还是在主线程中析沟。一定会发生拷贝构造),需谨慎处理
Thread t3(aFunc, 3, A(num)); // 此方式因为是在主线程中构造,所以此方式是安全的
// t3.join(); // 安全
t3.detach(); // 安全,但是detach时,若主线程已退出,那么子线程代码的执行行为会不确定。
/* 打印: (由于t3是detach的,所以每次执行结果可能会不一样,以下打印与理论上的执行输出相同)
主线程 threadId=140209374574400
one arg, this=0x7f8508e1ce94, threadId=140209356396288
A : 2, 0, threadId=140209356396288
destructor, this=0x7f8508e1ce94, threadId=140209356396288
one arg, this=0x7ffdda3e9f40, threadId=140209374574400
copy, this=0x7ffdda3e9ee0, threadId=140209374574400
copy, this=0x55f13408d6a8, threadId=140209374574400
destructor, this=0x7ffdda3e9ee0, threadId=140209374574400 // 注意这里是第一个拷贝构造函数,析沟在主线程中。。
A : 3, 0, threadId=140209356396288
destructor, this=0x55f13408d6a8, threadId=140209356396288
destructor, this=0x55f13408d6a8, threadId=140209356396288
*/
cout << "-------------------------------------类对象作thread函数参数 与 std::ref()---------------------------------------" << endl;
// 类对象作thread函数参数
A a(10);
Thread t4(bFunc, a); // 行为与 A(num) 行为一样
t4.join(); // 安全
// t4.detach(); // 安全
// 虽然在threah函数形参为A的引用,且函数中修改了m_num值,但是thread传参时发生的是拷贝,所以在thread中修改不会影响外部对象a
cout << "main a.num=" << a.m_num << endl; // main a.num=10
// std::ref():解决上述在thread函数中修改对象成员一致性的问题(原理是编译器传参时不会发生拷贝构造了,由于不会发生拷贝构造那么就存在栈区内存生命周期问题,detach就不安全了)
Thread t5(bFunc, std::ref(a)); // 使用 std::ref 后,thread函数形参可以不是const引用,对应的A的m_num也可以不用用mutable修饰
t5.join(); // 安全
// t5.detach(); // 不安全,由于使用了std::ref() 函数,而且此时a.m_num也不一定会被成功同步修改到外部对象a中
cout << "main a.num=" << a.m_num << endl; // main a.num=999 ,由于使用 std::ref 传参,所以在thread函数中修改a.m_num的值会同步到外部对象a中
cout << "------------------------------------std::ref() 同样适用于基础数据类型----------------------------------------" << endl;
// std::ref() 同样适用于基础数据类型
int c = 6;
Thread t6(cFunc, std::ref(c)); // 注意此时thread函数中的形参是非const引用
t6.join();
// t6.detach(); // 不安全,同上理
cout << "main num=" << c << endl; // main num=777 ,注意此时thread函数中的形参是非const引用
cout << "------------------------------------------智能指针作thread函数参数----------------------------------" << endl;
// 智能指针作thread函数参数, 智能指针必须使用make_函数创建。。这样可以防止莫名其妙的错误。
unique_ptr<A> pu = make_unique<A>(1);
shared_ptr<A> ps = make_shared<A>(2); // shared_ptr<>与thread混用时,若时使用 shared_ptr<A> ps(new (2)); 方式创建程序运行会抛宕。。 unique_ptr没事。。
Thread t7(dFunc, std::move(pu), ps); // 注意 pu 需要 std::move()
cout << "ps.use_cout=" << ps.use_count() << endl; // ps.use_cout=2
t7.join();
cout << "ps.use_cout=" << ps.use_count() << endl; // ps.use_cout=1
cout << "--------------------------------------类普通成员函数作thread函数--------------------------------------" << endl;
// 类普通成员函数作thread函数
A a8(8);
Thread t8(&A::threadFunc, a8, 1); // 注意看参数个数与传参方式 : 第一个参数取值符不能少
t8.join();
cout << "----------------------------------------------------------------------------" << endl;
Thread t9(&A::threadFunc, std::ref(a8), 1); // 与上述 std::ref() 效果一样。 此时 std::ref(a8) 可以用 &a8 取代,它们效果是一样的。
t9.join();
cout << "----------------------------------------------------------------------------" << endl;
Thread t10(std::ref(a8), 1); // 与上述 std::ref() 效果一样。 但是此时不能用 &a8 取代 std::ref(a8) 。。
t10.join();
return 0;
}