# Flow Control, Threads and Deadlock

When using flow control, you must be careful to avoid potential deadlock. When flow control is in effect for a destination, producers to that destination can block waiting for flow control signals from the destination’s consumers. If any of those consumers are within the same thread of program control, a potential for deadlock exists.

Namely, the producer will not unblock until the destination contains fewer messages, and the consumer in the blocked thread cannot reduce the number of messages.

The simplest case to detect is when producer and consumer are in the same session (sessions are limited to a single thread). But more complex cases can arise. Deadlock can even occur across several threads, or even programs on different hosts, if dependencies link them. For example, consider the situation in the following image that illustrates a flow control deadlock across two threads:

- Producer P1 in thread T1 has a consumer C2 in thread T2.
- Producer P2 in T2 has a consumer C1 in T1.
- Because of the circular dependency, deadlock can occur if either producer blocks its thread waiting for flow control signals.

The dependency analysis is analogous to mutex deadlock. You must analyze your programs and distributed systems in a similar way to avoid potential deadlock.

Copyright © Cloud Software Group, Inc. All rights reserved.