三种经典应用场景:
1 可变函数和参数
2 回调函数
3 取代虚函数
一、可变函数和参数
写一个函数,函数的参数是函数对象及参数,功能和thread类的构造函数相同。
下面demo涉及bind、可变参数、auto、decltype和完美转发
#include <iostream>
#include <thread>
#include <functional>
using namespace std;
void show1()
{
cout << "普通函数" << endl;
}
void show2(const string &msg)
{
cout << "带参数的普通函数" << msg << endl;
}
struct CC
{
void show3(int a, const string &b)
{
cout << "类的普通成员函数," << a << " " << b << endl;
}
};
/* //返回值 void
template <typename Fn, typename... Args>
void show(Fn fn, Args... args)
{
cout << "start show" << endl;
auto pf = bind(fn, args...); //利用bind的提前绑定
pf();
cout << "end show" << endl;
} */
/*
//返回函数对象
template <typename Fn, typename... Args>
auto show(Fn fn, Args... args) -> decltype(bind(fn, args...))
{
auto pf = bind(fn, args...); //利用bind的提前绑定
return pf;
} */
//最终版
//上面的两个示例,只支持拷贝语义,不支持移动语义,如果实参是右值,传着传着就丢了右值属性
template <typename Fn, typename... Args>
auto show(Fn &&fn, Args &&...args) -> decltype(bind(forward<Fn>(fn), forward<Args>(args)...))
{
cout << "start show" << endl;
auto pf = bind(forward<Fn>(fn), forward<Args>(args)...); //利用bind的提前绑定,且实用完美转发,保证属性不变
pf();
cout << "end show" << endl;
return pf;
}
int main()
{
// thread t1(show1);
// thread t2(show2, "hello");
// thread t3(&CC::show3, CC(), 1, "HELLO");
// t1.join();
// t2.join();
// t3.join();
show(show1);
show(show2, "hello");
show(&CC::show3, CC(), 1, "hello");
cout << "----有返回值----" << endl;
auto pf = show(show2, "hello");
pf();
return 0;
}
/*
start show
普通函数
end show
start show
带参数的普通函数hello
end show
start show
类的普通成员函数,1 hello
end show
----有返回值----
start show
带参数的普通函数hello
end show
带参数的普通函数hello */
二、回调函数的实现
在消息队列和网络库的框架中,但接收到消息(报文)时,回调用户自定义的函数对象,把消息(报文)参数传递给它,由它决定如何处理。
#include <iostream>
#include <thread>
#include <mutex>
#include <queue>
#include <chrono>
#include <condition_variable>
#include <functional>
using namespace std;
void show(const string &msg)
{
cout << "普通函数处理数据:" << msg << endl;
}
struct BB
{
void show(const string &msg)
{
cout << "类成员函数处理数据:" << msg << endl;
}
};
class AA
{
mutex m_mutex;
condition_variable m_cond;
queue<string> m_q;
function<void(const string &)> m_callback; //回调函数对象
public:
template <typename Fn, typename... Args>
void callback(Fn &&fn, Args &&...args) //可变参数,主要是为了适配类的普通成员函数
{
m_callback = bind(forward<Fn>(fn), forward<Args>(args)..., placeholders::_1);
}
void incache(int num)
{
lock_guard<mutex> lock(m_mutex);
for (int i = 0; i < num; i++)
{
static int id = 1;
string msg = to_string(id++) + "号选手";
m_q.push(msg);
}
m_cond.notify_all();
}
void outcache()
{
while (true)
{
unique_lock<mutex> lock(m_mutex);
m_cond.wait(lock, [this]
{ return !m_q.empty(); });
string msg = m_q.front();
m_q.pop();
cout << "线程:" << this_thread::get_id() << "," << msg << endl;
lock.unlock();
if (m_callback)
m_callback(msg);
}
}
};
void test()
{
AA aa;
// aa.callback(show);
BB bb;
aa.callback(&BB::show, &bb);
thread t1(&AA::outcache, &aa);
thread t2(&AA::outcache, &aa);
thread t3(&AA::outcache, &aa);
this_thread::sleep_for(chrono::seconds(2));
aa.incache(2);
this_thread::sleep_for(chrono::seconds(3));
aa.incache(5);
t1.join();
t2.join();
t3.join();
}
int main()
{
test();
return 0;
}
开发中,普通函数能做的事情非常有限,在现代C++中,回调函数一般用类的成员函数,足够强大
三、如何取代虚函数
C++面向对象的三大特征:封装、继承和多态
封装的意义是很大的,这是共识,继承和多态就不好说,争议非常大,特别是多态,在有些大佬看来,那简直是多余,这话也是有一些道理的。
虚函数在执行的过程中会跳转两次(先去查找虚函数表,然后再通过虚函数表找到真正的执行地址),这样的话,CPU会跳转两次,而普通函数只跳转一次。
CPU每跳转一次,预取指令要作废很多,所以效率比较低,查找虚函数表,问题可能不大,预取指令作废是导致函数效率不高的关键。(百度了解详情)
说虚函数效率低,其实也不会低太多,普通的系统用虚函数不会有什么问题,但是,对性能要求极高的系统来说,这种性能的损失不可接受。
包装器和绑定器也可以实现虚函数的功能,并且不会损失性能。
#include <iostream>
#include <functional>
using namespace std;
//取代虚函数
struct Hero
{
// virtual void show() { cout << "英雄释放了技能" << endl; }
function<void()> m_callback;
template <typename Fn, typename... Args>
void callback(Fn &&fn, Args &&...args)
{
m_callback = bind(forward<Fn>(fn), forward<Args>(args)...);
}
//再写个show函数取代曲函数,调用子类的成员函数
void show()
{
m_callback();
}
virtual ~Hero() { cout << "~Here()" << endl; }
};
struct XS : public Hero
{
~XS() { cout << "~XS()" << endl; }
void show() { cout << "西施释放了技能" << endl; }
};
struct HX : public Hero
{
~HX() { cout << "~HX()" << endl; }
void show() { cout << "韩信释放了技能" << endl; }
};
void test()
{
int id = 0;
cout << "请输入英雄:1:西施,2:韩信" << endl;
cin >> id;
Hero *ptr = nullptr;
if (id == 1)
{
ptr = new XS;
// ptr->callback(&XS::show, (XS *)ptr);//ptr是基类指针,要转换为派生类指针,C语言转换风格
ptr->callback(&XS::show, static_cast<XS *>(ptr)); // C++风格
}
else if (id == 2)
{
ptr = new HX;
ptr->callback(&HX::show, (HX *)ptr);
}
if (ptr != nullptr)
{
ptr->show();
delete ptr;
}
}
int main()
{
test();
return 0;
}
包装器和绑定器,不要求两个类之间是否有继承关系,这个demo保留了,保留了继承关系,是为了方便处理指针