c11的一些新特性
1 |
|
1 | template |
decltype(v) //返回v的数据类型 v可以是基础类型指针 函数指针 函数 …
std::result_of<>//返回可调用类型的返回值
1
2
3
4
5using TIF = std::function<int()>;
TIF tif = []() -> int{};
typedef std::result_of<TIF()>::type TA; //参数是类型 跟decltype类似std::future<数据类型> 对数据的封装
std::async<>//同步调用线程
1
2
3
4
5
6
7std::future<int> fut = std::async([]()->int{
std::cout<<"async"<<std::endl;
std::this_thread::sleep_for(std::chrono::seconds(5));
return 111;
});
std::cout<<"async return:"<<fut.get()<<std::endl;std::promise<数据类型>//对值得封装 //主要用于并发中的数据传递
1
2
3
4
5
6
7
8
9std::promise<int> pro;
std::future<int> f = pro.get_future();
std::thread th8([](std::future<int>&f){
std::cout<<"get future value:"<<f.get()<<std::endl;
},std::ref(f));
pro.set_value(999);std::packaged_task<函数>//对方法得封装 //主要用于并发中的方法 多线程中如果不把task放到多线程中会阻塞。
1
多线程中传递方法
c++11后使用operator””自定义类型
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16class OperatorTest11{
public:
OperatorTest11(){}
std::string GetName(){return "Func Jay";}
};
// 参数是固定的, 不能少
inline OperatorTest11 operator""_jay(const char * name,size_t n) {
return OperatorTest11();
}
main() {
auto opt_fun = "name"_jay;
std::cout<<"Name"<<opt_fun.GetName()<<std::endl;
}
c++ 惯用手法
New
std::chrono::time_point
std::reference_wrapper
compare_exchange_weak
compare_exchange_strong
std::this_thread::yield();
std::underlying_type();
attribute((aligned(64)));
template <typename… Targs> 多种类型构造封装
CAS机制:
CAS的基本思想是认为当前环境中的并发并没有那么高,比较乐观的看待整个并发,只需要在更新某个值时先检查下该值有没有发生变化,如果没有发生变化则更新,否则放弃更新。
CAS(compare and wasp)比较并操作,解决多线程并行情况下使用锁造成性能损耗的机制
RAII:
获取资源即初始化。将资源于对象绑定(构造函数获取资源,析构函数释放资源,资源)
RAII的本质内容是用对象代表资源,把管理资源的任务转化为管理对象的任务,将资源的获取和释放与对象的构造和析构对应起来,从而确保在对象的生存期内资源始终有效,对象销毁时资源必被释放。换句话说,拥有对象就等于拥有资源,对象存在则资源必定存在。由此可见,RAII惯用法是进行资源管理的有力武器。
常用封装 RAII expeted&error all_calss
1 |
|
c++11 future
1 | 1. enable_shared_from_this |