# Do not run custom targets in parallel

**URL:** https://discourse.cmake.org/t/do-not-run-custom-targets-in-parallel/1353
**Category:** Usage
**Created:** [June 8, 2020, 5:35pm UTC](https://discourse.cmake.org/t/do-not-run-custom-targets-in-parallel/1353 "2020-06-08T17:35:55Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![KUGA2](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kuga2/32/4968_2.png) [@KUGA2](https://discourse.cmake.org/u/KUGA2)
#### Post date: [June 8, 2020, 5:35pm UTC](https://discourse.cmake.org/t/do-not-run-custom-targets-in-parallel/1353/1 "2020-06-08T17:35:55Z")

</div>

I have a custom function that creates a custom target with a custom command. This custom command uses a common file (changes it and restores it).

Now, there is a race condition. If two of those targets run at the same time the file is modified by both target resulting in wrong outputs.  
I cannot make them depend on each other, because they should not know of each other.  
I cannot copy the file, it needs to stay at the same location.

Tests have a property `RUN_SERIAL` which would be great but well, its a target not a test. There is also `file(LOCK)` but this cannot be used since it would run in different processes which it does not support according to documentation.

Do you know a way of doing this?

---

<div class="post-metadata">

### Author: ![kyle.edwards](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kyle.edwards](https://discourse.cmake.org/u/kyle.edwards)
#### Post date: [June 8, 2020, 5:45pm UTC](https://discourse.cmake.org/t/do-not-run-custom-targets-in-parallel/1353/2 "2020-06-08T17:45:49Z")

</div>

Run the build in serial: `ninja -j1` or `make -j1`.

Alternatively, change your custom commands to not use a common file, or to establish a lock on it so that only one modifies it at a time.

---

<div class="post-metadata">

### Author: ![kyle.edwards](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kyle.edwards](https://discourse.cmake.org/u/kyle.edwards)
#### Post date: [June 8, 2020, 5:47pm UTC](https://discourse.cmake.org/t/do-not-run-custom-targets-in-parallel/1353/3 "2020-06-08T17:47:54Z")

</div>

> [@KUGA2](#):
>
> There is also `file(LOCK)`

There are other ways of locking files. It’s a very common problem for daemons. I’d suggest taking a look at how other software locks files.

---

<div class="post-metadata">

### Author: ![KUGA2](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kuga2/32/4968_2.png) [@KUGA2](https://discourse.cmake.org/u/KUGA2)
#### Post date: [June 19, 2020, 8:10am UTC](https://discourse.cmake.org/t/do-not-run-custom-targets-in-parallel/1353/4 "2020-06-19T08:10:24Z")

</div>

Sure, this is possible, but it is hard to get this platform independent.
