Lambda Expression Follow-up¶

  • Homework to use a lambda expression with a standard algorithm or container
set<int, ???> s; // How do you use a lambda for a comparison type?
  • You cannot
    • Best you can do is use a pointer to function or std::function and pass the lambda to the constructor as comparison
{
    set<int, bool (*)(int a, int b)> s{[](int a, int b) { return a > b; }};
}
  • Disadvantage is the expression cannot be inlined
  • Recommendation - continue to use function objects
{
    set<int, greater<>> s;
}

Memory Order Follow-up¶

  • It wasn't clear in my slides that C++11 allows you to specify the memory order on many operations
atomic<int> x;
x.store(10, memory_order_relaxed);
  • You can specify the memory order for
    • operations on atomics
    • explicitly with std::atomic_thread_fence, std::atomic_signal_fence
    • and can explicitly control dependencies with std::kill_dependency
  • The memory order controls
    • How the compiler can reorder operations on possibly shared memory around the atomic operations
    • Which instructions the compiler emits to control processor memory ordering around the atomic operations