Professional C__ - Marc Gregoire [466]
1. Use message-based debugging: When debugging multithreaded programs, message-based debugging can be more effective than using a debugger. Depending on your application, you can implement message-based debugging by writing messages to cout, a log file, a GUI control and so on. Add debug statements to your program before and after critical sections, and before acquiring and after releasing locks. Often by watching this output, you will be able to detect deadlocks and race conditions because you will be able to see that two threads are in a critical section at the same time or that one thread is stuck waiting for a lock.
2. Insert forced sleeps and context switches: If you are having trouble reproducing the problem consistently, or have a hunch about the root cause but want to verify it, you can force certain thread-scheduling behavior by making your threads sleep for specified amounts of time. The C++11 Debugging Example: Article Citations This section presents a buggy program and shows you the steps to take in order to debug it and fix the problem. Suppose that you’re part of a team writing a web page that allows users to search for the research articles that cite a particular paper. This type of service is useful for authors who are trying to find work similar to their own. Once they find one paper representing a related work, they can look for every paper that cites that one to find other related work. In this project, you are responsible for the code that reads the raw citations data from text files. For simplicity, assume that the citation info for each paper is found in its own file. Furthermore, assume that the first line of each file contains the author, title, and publication info for the paper; the second line is always empty; and all subsequent lines contain the citations from the article (one on each line). Here is an example file for one of the most important papers in Computer Science: Alan Turing,"On Computable Numbers with an Application to the Entscheidungsproblem", Proceedings of the London Mathematical Society, Series 2, Vol.42 (1936 - 37) pages 230 to 265. Godel, "Uber formal unentscheidbare Satze der Principia Mathernatica und verwant der Systeme, I", Monatshefte Math. Phys., 38 (1931). 173-198. Alonzo Church. "An unsolvable problem of elementary number theory", American J of Math., 58(1936), 345 363. Alonzo Church. "A note on the Entscheidungsprob1em", J. of Symbolic logic, 1 (1930), 40 41. Cf. Hobson, "Theory of functions of a real variable (2nd ed., 1921)", 87, 88. Proc. London Math. Soc (2) 42 (1936 7), 230 265. Buggy Implementation of an ArticleCitations Class You decide to structure your program by writing an ArticleCitations class that reads the file and stores the information. This class stores the article info from the first line in one string, and the citations’ info in an array of strings. Please note that this design decision is not the best possible. You should opt for one of the STL containers to store the citations. However, for the purpose of illustrating buggy applications, it’s perfect. The class definition looks like this: #include using std::string; class ArticleCitations { public: ArticleCitations(const string& fileName); virtual ~ArticleCitations(); ArticleCitations(const ArticleCitations& src);