Dmalloc Logo


Search this site:

If dmalloc has saved you or your company time or money, please use your credit-card or PayPal account to donate to the cause.

Dmalloc Tutorial: 3.9 Using the Library with a Thread Package
[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.9 Using the Library with a Thread Package

Threads are special operating system facilities which allow your programs to have multiple threads of execution (hence the name). In effect your program can be doing a number of things "at the same time". This allows you to take full advantage of modern operating system scheduling and multi-processor hardware. If I've already lost you or if any of the terminology below does not make sense, see manuals about POSIX threads (pthreads) before going any further. O'Reilly publishes a pretty good pthreads manual for example.

To use dmalloc with your threaded program, you will first need to make sure that you are linking with `libdmallocth.a' which is the threaded version of the library. The support for threads in dmalloc should be adequate for most if not all testing scenarios. It provides support for mutex locking itself to protect against race conditions that result in multiple simultaneous execution. One of the major problems is that most thread libraries uses malloc themselves. Since all of dmalloc's initialization happens when a call to malloc is made, we may be attempting to initialize or lock the mutex while the thread library is booting up. A very bad thing since thread libraries don't expect to recurse.

The solution to this problem is to have the library not initialize or lock its mutex variable until after a certain number of allocation calls have been completed. If the library does not wait before initializing the locks, the thread library will probably core dump. If it waits too long then it can't protect itself from multiple execution and it will abort or other bad things might happen. You adjust the number of times to wait at runtime with the `lock-on' option to the dmalloc program (for example dmalloc -o 20). See section Dmalloc Utility Program. Times values between 5 and 30 are probably good although operating systems will vary significantly. You know its too low if your program immediately core dumps and too high if the dmalloc library says its gone recursive although with low values, you might get either problem.

An additional complexity is when we are initializing the lock before mutex locking around the library. As mentioned, the initialization itself may generate a malloc call causing the library to go recursive and the pthread library to possibly core dump. With the THREAD_INIT_LOCK setting defined in `settings.h', you can tune how many times before we start locking to try and initialize the mutex lock. It defaults to 2 which seems to work for me. If people need to have this runtime configurable or would like to present an alternative default, please let me know.

So to use dmalloc with a threaded program, follow the following steps carefully.

  1. Follow the installation instructions on how to configure, make, and install the library but make sure to add the --enable-threads argument to configure. See section How to Install the Library.
  2. Typing make should be enough to build the threaded versions of the libraries including `libdmallocth.a'.
  3. Link the dmalloc threaded library into your program. The dmalloc library should probably be placed at or near the end of the library list.
  4. Enable the debugging options that you need by typing dmalloc -l logfile -i 100 low (for example). dmalloc --usage will provide verbose usage info for the dmalloc program. See section Dmalloc Utility Program.
  5. Enable the "lock-on" option (for example dmalloc -o 20). As explained above, you may have to try different values before getting it right. Values between 5 and 30 are probably good.
  6. If you get a dmalloc error #13 `thread locking has not been configured' then you have not compiled you program with the threaded version of dmalloc or there was a problem building it.
  7. If everything works, you should be able to run your program, have it not immediately crash, and the dmalloc library should not complain about recursion.

If you have any specific questions or would like addition information posted in this section, please let me know. Experienced thread programmers only please.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]

This document was generated by Gray Watson on May, 16 2007 using texi2html 1.76.

This work is licensed by Gray Watson under the Creative Commons Attribution-Share Alike 3.0 License.
This page should be W3C Valid XHTML and should work with most browsers.