Working with Asynchrony Generically: A Tour of C++ Executorscompute_intensive(int); int main() { unifex::static_thread_pool pool{8}; ex::scheduler auto sched = pool.get_scheduler(); ex::sender auto work = ex::when_all( ex::then(ex::schedule(sched), [] { return compute_intensive(0); compute_intensive(1); }), ex::then(ex::schedule(sched), [] { return compute_intensive(2); }) ); auto [a, b, c] = std::this_thread::sync_wait( std::move(work) ).value(); } Launch three tasks to execute compute_intensive(int); int main() { unifex::static_thread_pool pool{8}; ex::scheduler auto sched = pool.get_scheduler(); ex::sender auto work = ex::when_all( ex::then(ex::schedule(sched), [] { return compute_intensive(0);0 码力 | 121 页 | 7.73 MB | 6 月前3
C++23: An Overview of Almost All New and Updated FeaturesMultidimensional Subscript Operator Attributes on Lambda-Expressions Literal Suffix for size_t auto(x): decay-copy in The Language #elifdef, #elifndef, and #warning Marking Unreachable Code Multidimensional Subscript Operator Attributes on Lambda-Expressions Literal Suffix for size_t auto(x): decay-copy in The Language #elifdef, #elifndef, and #warning Marking Unreachable Code Explicit object parameters deducing this Replace all 3 overloads with: templateauto&& GetName(this Self&& self) { return std::forward (self).m_name; }11 Explicit Object Parameters 0 码力 | 105 页 | 759.96 KB | 6 月前3
C++高性能并行编程与优化 - 课件 - 17 由浅入深学习 map 容器运用举例:出现次数统计 • vectorinput = {“hello”, “world”, “hello”}; • map counter; • for (auto const &key: input) { • counter[key]++; } • 则 counter 最后为: • {“hello”: 2, “world”: 1}; [] 运用举例:归类 vector input = {“happy”, “world”, “hello”}; • map > categories; • for (auto const &str: input) { • char key = str[0]; • categories[key].push_back(str); } • 则 categories 找不到时,自动采用默认值 • 要求:当 m 中有 “ key” 时返回 key 对应的值,否则返回指定的默认值 “ default” 。 • 可以用 count 和 at 的组合拳。 • auto val = m.count(“key”) ? m.at(“key”) : “default”; 找不到时,自动采用默认值 • template • typename M::mapped_type 0 码力 | 90 页 | 8.76 MB | 1 年前3
C++20: An (Almost) Complete Overviewexport module cppcon; // Module declaration namespace CppCon { auto GetWelcomeHelper() { return "Welcome to CppCon 2020!"; } export auto GetWelcome() { return GetWelcomeHelper(); } } Consume a module: 2, 3, 4, 5, 6, 7, 8, 9, 10 }; auto result { data | views::filter([](const auto& value) { return value % 2 == 0; })/* 2 4 6 8 10 */ | views::transform([](const auto& value) { return value * 2.0; done until you iterate over result Working with infinite sequences: auto result { view::ints(10) | views::filter([](const auto& value) { return value % 2 == 0; }) /* ... */ | views::take(10)0 码力 | 85 页 | 512.18 KB | 6 月前3
C++高性能并行编程与优化 - 课件 - 03 现代 C++ 进阶:模板元编程为什么需要自动类型推导( auto ) 没有 auto 的话,需要声明一个变量,必须重复一遍他的类型,非常麻烦 : 自动类型推导:定义变量 因此 C++11 引入了 auto ,使用 auto 定义的变量,其类型会自动根据等号右边的值来确定 : 自动类型推导:一些局限性 • 不过 auto 也并非万能,他也有很多限制。 • 因为需要等号右边的类型信息,所以没有 = 单独声明一个 auto 变量是不行的: 而且,类成员也不可以定义为 auto : 自动类型推导:函数返回值 • 除了可以用于定义变量,还可以用作函数的返回类型: • 使用 auto 以后,会自动被推导为 return 右边的类型。 • 不过也有三点注意事项: 1. 当函数有多条 return 语句时,所有语句的返回类型必须一致,否则 auto 会报错。 2. 当函数没有 return 语句时, auto 会被推导为 void 。 。 3. 如果声明和实现分离了,则不能声明为 auto 。比如: auto func(); // 错误 C++ 特性:引用( int & ) • 众所周知, C++ 中有一种特殊的类型,叫做引用。只需要在原类型后面加一个 & 即可。 • 引用的本质无非是指针,当我们试图修改一个引用时,实际上是修改了原来的对象: 等价于 : 可见,和 C 语言的 int * 相比 无非是减少了 & 和 *0 码力 | 82 页 | 12.15 MB | 1 年前3
A Crash Course in Calendars, Dates, Time, and Time Zonesduration> d8 { d7 }; // minutes // Error! // Force conversion (0 instead of 0.5) auto d8 { duration_cast >>(d7) }; // = 015 Durations – Predefined & Literals ratio<60>> d9 { 10 }; // minutes Equivalent to: minutes d9 { 10 }; Or: auto d9 { 10min }; Example: auto t { hours { 1 } + minutes { 23 } + seconds { 45 } };18 Durations – Predefined & Get current time as a time_point. system_clock::time_point tpoint { system_clock::now() }; // Or: auto tpoint { system_clock::now() }; // Convert to a time_t. time_t tt { system_clock::to_time_t(tpoint) 0 码力 | 43 页 | 551.60 KB | 6 月前3
C++20's std::chrono; int main() { year y{2021}; std::cout << y << "\n"; month m{October}; auto result = m + months{3}; std::cout << result << "\n"; } Output: 2021 Jan12 Some examples:#include using namespace std::chrono; int main() { weekday wd{Thursday}; auto result = wd + days{4}; std::cout << result << "\n"; weekday sun1{0}; weekday sun2{7}; std::chrono; int main() { try { auto ld = local_days{Sunday[1]/November/ 2016}; auto lt = ld + 1h + 30min; auto zt = zoned_time{"America/ New_York", lt}; } catch 0 码力 | 55 页 | 8.67 MB | 6 月前3
C++20 STL Features: 1 Year of Development on GitHubdll"sv, ".exe"sv, ".obj"sv}; static_assert(ranges::is_sorted(skipped_extensions)); for (const auto& ent : filesystem::recursive_directory_iterator{"."}) { const string extension = ent.path() { for (const auto& e : v) { cout << e << " "; } cout << "\n"; } int main() { vectorv{"bear", "dog", "cat", "lion", "ox", "dog"}; auto pred = [](const auto& s) { return s.size() using namespace std; void print(const span s) { // not a template! for (const auto& e : s) { cout << e << " "; } cout << endl; } int main() { static constexpr int classic[]{1 0 码力 | 45 页 | 702.09 KB | 6 月前3
Lock-Free Atomic Shared Pointers Without a Split Reference Count? It Can Be Done!push_front(T t) { auto p = make_shared(std::move(t), head.load()); while (!head.compare_exchange_weak(p->next, p)) {} } optional pop_front() { auto p = head.load(); danielanderson.net The problem atomic ref_count = 1; … T atomic > a Thread 1 auto s = a.load(); Thread 2 a.store(make_shared ( …)); ++ -- If this happens first…23 The fundamental fundamental problem: The race to zero Thread 1 load(): Thread 2 store(shared_ptr desired): auto cb = ctrl.load(); Need a way to ensure that cb is not deleted before we increment the ref 0 码力 | 45 页 | 5.12 MB | 6 月前3
FlexClassmy::shared_ptr(block)I will write it myself ™ Control Block T T T T T T … template auto my::make_shared(int n) { // Allocate sizeof(controlBlock) + n*sizeof(T) bytes struct Block { auto fc_handles() { return fc::make_tuple(&data); } int size; int ref_cnt; fc::AdjacentArray data; }; template auto my::make_shared(int n) https://godbolt.org/z/v3hdhYnaT auto begin = block->data.begin(); auto end = std::advance(begin, block- >size);Features #include struct Foo { auto fc_handles() { return fc::make_tuple(&h1 0 码力 | 8 页 | 957.56 KB | 6 月前3
共 25 条
- 1
- 2
- 3













