Presentation is loading. Please wait.

Presentation is loading. Please wait.

E81 CSE 532S: Advanced Multi-Paradigm Software Development Chris Gill Department of Computer Science and Engineering Washington University in St. Louis.

Similar presentations


Presentation on theme: "E81 CSE 532S: Advanced Multi-Paradigm Software Development Chris Gill Department of Computer Science and Engineering Washington University in St. Louis."— Presentation transcript:

1 E81 CSE 532S: Advanced Multi-Paradigm Software Development Chris Gill Department of Computer Science and Engineering Washington University in St. Louis cdgill@cs.wustl.edu Advanced C++11 Thread Management

2 Thread Pools Simplest version –A thread per core, all run a common worker thread function Waiting for tasks to complete –Promises and futures give rendezvous with work completion –Could also post work results on an active object’s queue, which also may help avoid cache ping-pong –Futures also help with exception safety, e.g., a thrown exception propagates to thread that calls get on the future Granularity of work is another key design decision –Too small and the overhead of managing the work adds up –To coarse and responsiveness, concurrency, may suffer Work stealing lets idle threads relieve busy ones –May need to hand off promise as well as work, etc.

3 Interrupting Threads (Part I) Thread with interruption point is (cleanly) interruptible –Another thread can set a flag that it will notice and then exit –Clever use of lambdas, promises, move semantics lets a thread-local interrupt flag be managed (see listing 9.9) –Need to be careful to avoid dangling pointers on thread exit For simple cases, detecting interruption may be trivial –E.g., event loop with interruption point checked each time For condition variables interruption is more complex –E.g., using the guard idiom to avoid exception hazards –E.g., waiting with a timeout (and handling spurious wakes) –Can eliminate spurious wakes with a scheme based on a custom lock and a condition_variable_any (listing 9.12)

4 Interrupting Threads (Part II) Unlike condition variable waits, thread interruption with other blocking calls goes back to timed waiting –No access to internals of locking and unlocking semantics –Best you can do is unblock and check frequently (with interval chosen to balance overhead and responsiveness) Handling interruptions –Can use standard exception handling in interrupted thread –Can use promises and futures between threads to propagate –Put a catch block in the wrapper that initializes the interrupt flag, so uncaught exception doesn’t end the entire program Can combine interruption and joining with threads –E.g., to stop background threads and wait for them to end


Download ppt "E81 CSE 532S: Advanced Multi-Paradigm Software Development Chris Gill Department of Computer Science and Engineering Washington University in St. Louis."

Similar presentations


Ads by Google