Yale Zhang
Yale Zhang

Reputation: 1578

Does GNU make suffer from race conditions when building targets that have common dependencies?

I have a simple example makefile that shows my problem:

.PHONY: a.out b.out

all: a.out b.out

common:
    echo building common
    sleep 1
    touch common

a.out: common
    echo building a.out
b.out: common
    echo building b.out

a.out & b.out depend on common, so there can be a race condition (common being generated twice) when doing a parallel build.

I did make -j4 and didn't experience common being generated twice. I even put a sleep statement in the generation of common to make things more deterministic.

So is it safe to say that GNU make properly synchronizes when building common dependencies?

Upvotes: 3

Views: 2182

Answers (1)

MadScientist
MadScientist

Reputation: 100856

Yes, GNU make's parallelization support handles this properly (it would not be very useful if it didn't!) In general, if your makefile environment completely and correctly declares your dependency graph to make, then make will always build it correctly regardless of the number of parallel jobs.

The trick is that for some kinds of complicated environments, it can be difficult to completely and correctly declare the dependency graph. But for simple situations like above, there's no problem.

Upvotes: 5

Related Questions