Common Oversights

The following list points out some of the more frequent oversights that can cause bugs in multithreaded programs.

Passing a pointer to the caller's stack as an argument to a new thread.

Accessing global memory (shared changeable state) without the protection of a synchronization mechanism.

Creating deadlocks caused by two threads trying to acquire rights to the same pair of global resources in alternate order (so that one thread controls the first resource and the other controls the second resource and neither can proceed until the other gives up).

Trying to reacquire a lock already held (recursive deadlock).

Creating a hidden gap in synchronization protection. This is caused when a code segment protected by a synchronization mechanism contains a call to a function that frees and then reacquires the synchronization mechanism before it returns to the caller. The result is that it appears to the caller that the global data has been protected when it actually has not.

Mixing UNIX signals with threads--it is better to use the sigwait(2) model for handling asynchronous signals.

Using setjmp(3B) and longjmp(3B), and then long-jumping away without releasing the mutex locks.

Failing to reevaluate the conditions after returning from a call to *_cond_wait(3T) or *_cond_timedwait(3T).

Forgetting that default threads are created PTHREAD_CREATE_JOINABLE and must be reclaimed with pthread_join(3T); note, pthread_exit(3T) does not free up its storage space.

Making deeply nested, recursive calls and using large automatic arrays can cause problems because multithreaded programs have a more limited stack size than single-threaded programs.

Specifying an inadequate stack size, or using non-default stacks.

And, note that multithreaded programs (especially those containing bugs) often behave differently in two successive runs, given identical inputs, because of differences in the thread scheduling order.

In general, multithreading bugs are statistical instead of deterministic. Tracing is usually a more effective method of finding order of execution problems than is breakpoint-based debugging.