![]() #include "stdafx.h"Ĭout lock(mu) // Start of Critical Section - to protect std::coutĬout << "Thread " << this_thread::get_id() << " says " << message << endl In the example below, we forgot to join t1 to the main thread. If we forgot to join a thread or detach it(make it unjoinable) before the main program terminates, it'll cause in a program crash. Mistake # 1: Not using join() to wait for background threads before terminating an application If you know any more pitfalls, or have alternative suggestions for some of the mistakes – please leave a comment below and I'll factor them into the article. In this article, I've tried to catalog all the mistakes I know of, with potential solutions. Most of these mistakes were luckily caught in code review and testing however, some arcane ones did slip through and make it into production code and we had to patch live systems, which is always expensive. I've made a number of mistakes myself over the years. Threading is one of the most complicated things to get right in programming, especially in C++.
0 Comments
Leave a Reply. |