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 Managing C++11 Threads

2 Contruct a Thread to Launch It The std::thread constructor takes any callable type –A function, function pointer, function object, or lambda –This includes things like member function pointers, etc. –Additional constructor arguments passed to callable instance Watch out for argument passing semantics, though –Constructor arguments are copied locally without conversion –Need to wrap references in std::ref, force conversions, etc. Default construction (without a thread) also possible –Can transfer ownership of it via C++11 move semantics, i.e., using std::move from one std::thread object to another – Be careful not to move thread ownership to a std::thread object that already owns one (terminates the program)

3 Always Join With or Detach a Launched Thread Often you should join with each launched thread –E.g., to wait until a result it produces is ready to retrieve –E.g., to keep a resource it needs available for its lifetime However, for truly independent threads, can detach –Relinquishes parent thread’s option to rendezvous with it –Need to copy all resources into the thread up front –Avoids circular wait deadlocks (if A joins B and B joins A) Need to ensure join or detach for each thread –E.g., if an exception is thrown, still need to make it so –The guard (a.k.a. RAII) idiom helps with this, since guard’s destructor always joins or detaches if needed –The std::thread::joinable() method can be used to test that


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