The rationale for POSIX
Because no error is produced if any path component already exists, the -p option is also useful to ensure that a particular directory exists.
This suggests that parallel use should be safe. I can understand your doubts, given the potential for races, so let’s look at an implementation...
-p option ends up being handled by
mkancesdirs, which has a helpful description which should allay your fears:
Ensure that the ancestor directories of FILE exist, using an
algorithm that should work even if two processes execute this
function in parallel.
(Instead of immediately acting on errors from
mkdir, it tries to
chdir into each component directory and only fails if that fails.)
If you’re using GNU Make, you could use its secondary expansion to handle directories in the same way as other prerequisites:
mkdir -p $@
out/%.x: $$(@D)/ ...