std async
play

std::async Standard C++ function template Supposed to make threads - PowerPoint PPT Presentation

a sync m agic (en)light e ning talk trieger@informatik.hu-berlin.de async as a code reordering specifier Everything else is just multithreading EuroLLVM14 Tobias Rieger 1 std::async Standard C++ function template Supposed to make


  1. a sync m agic (en)light e ning talk trieger@informatik.hu-berlin.de async as a code reordering specifier Everything else is just multithreading EuroLLVM14 Tobias Rieger 1

  2. std::async ● Standard C++ function template ● Supposed to make threads (easier) ● Already have std::thread , pthread , boost::thread , … ● Takes functions, function objects and lambdas with an arbitrary number of parameters – No void * shenanigans ● Returns a future with a problem ● Optional first parameter – std::launch::async → Creates a thread – std::launch::deferred → Does not create a thread – std::launch::async | std::launch::deferred → Let the compiler decide EuroLLVM14 Tobias Rieger 2

  3. Reordering void foo(){ void foo(){ //pseudo code somelib::doStuffs(); if (cout_mutex.try_lock()){ cout << “Hi there!\n”; cout _withou_lock << “Hi there!\n”; } cout_mutex.unlock(); somelib::doStuffs(); } else{ void foo(){ somelib::doStuffs(); auto f = async( cout << “Hi there!\n”; somelib::doStuffs(); } ); } cout << “Hi there!\n”; f.wait(); } Also works for volatile memory accesses, mutexes and atomics EuroLLVM14 Tobias Rieger 3

  4. Magic async can make single threaded code faster! int var; int spill(){ var = 5; Thread semantic without threads somelib::doSomething(); return var + 7; } int var; Data race free → int spill(){ somelib::doSomething var = 5; must not access var → auto f = async([&]{var = var;}); reordering possible → somelib::doSomething(); performance → f.wait(); return var + 7; } EuroLLVM14 Tobias Rieger 4

  5. Intend and result What the C++ standard What the C++ standard committee specified: committee realized later on: ● Templates are Turing-complete ● Templates for generic → Language in a language programming ● Redefinition of const to mean ● const correct STL thread safe ● Specification of a code ● async for threads reordering specifier EuroLLVM14 Tobias Rieger 5

  6. Implementation Bad implementation Few uses it Not worth putting effort into EuroLLVM14 Tobias Rieger 6

  7. Implementation Good Everyone Bad implementation implementation Few uses it uses it Not worth putting LLVM effort into EuroLLVM14 Tobias Rieger 7

Recommend


More recommend