# Could anyone explain how InterProcessLock works?

**URL:** <https://forum.juce.com/t/could-anyone-explain-how-interprocesslock-works/52512>\
**Category:** General JUCE discussion\
**Created:** [August 7, 2022, 10:58pm UTC](https://forum.juce.com/t/could-anyone-explain-how-interprocesslock-works/52512 "2022-08-07T22:58:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wiznat](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/wiznat/32/11103_2.png) [@Wiznat](https://forum.juce.com/u/Wiznat)\
**Post date:** [August 7, 2022, 10:58pm UTC](https://forum.juce.com/t/could-anyone-explain-how-interprocesslock-works/52512/1 "2022-08-07T22:58:28Z")

</div>

I think I know how to use it, just looking for a better understanding.

Like right now, I have a save file function, and looks something like

```auto
    const InterProcessLock::ScopedLockType sl (saveLock);
    
    saveFile.deleteFile();
    FileOutputStream os(saveFile);
    dataTree.writeToStream(os);

```

and in the header

```auto
private:
InterProcessLock saveLock{"saveLock"};

```

How does interProcessLock/ scopedLock even know what to lock down?  
And I guess while were here is this related to criticalSections ? I also don’t quite understand what the criticalSection is/does.

Any input appreciated! Thanks.

---

<div class="post-metadata">

**Author:** ![anon48770766](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon48770766](https://forum.juce.com/u/anon48770766)\
**Post date:** [August 8, 2022, 7:40am UTC](https://forum.juce.com/t/could-anyone-explain-how-interprocesslock-works/52512/2 "2022-08-08T07:40:46Z")

</div>

> [@Wiznat](#):
>
> How does interProcessLock/ scopedLock even know what to lock down?

It doesn’t. You tell it, by using it in the scope where its needed. Taking the lock means any other code requesting it will have to wait until you’ve released it - so, be careful with the scope where you apply the lock, and be sure to only do ‘critical’ things while you are holding the lock.

criticalSections are kind of important. You should probably get familiar with the basics … they are also critical areas of the code that you must protect from re-entry - with a lock, for example (but its not the only way to protect critical sections - see also semaphores and mutexes). Therefore they are related to your understanding of the ScopedLock, too - you should definitely try to understand the ways that scoped code should be treated, critically …

> **[Critical section](https://en.wikipedia.org/wiki/Critical_section)**
>
> In concurrent programming, concurrent accesses to shared resources can lead to unexpected or erroneous behavior, so parts of the program where the shared resource is accessed need to be protected in ways that avoid the concurrent access. One way to do so is known as a critical section or critical region. This protected section cannot be entered by more than one process or thread at a time; others are suspended until the first leaves the critical section. Typically, the critical section accesses ...

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [August 8, 2022, 12:49pm UTC](https://forum.juce.com/t/could-anyone-explain-how-interprocesslock-works/52512/3 "2022-08-08T12:49:35Z")

</div>

> [@Wiznat](#):
>
> ```auto
> const InterProcessLock::ScopedLockType sl (saveLock);
>     
> saveFile.deleteFile();
> FileOutputStream os(saveFile);
> dataTree.writeToStream(os);
> 
> ```

Instead of locking you could use the [juce::TemporaryFile](https://docs.juce.com/master/classTemporaryFile.html#details).  
You write to a unique temporary sibling and once you are done it is moved as an atomic filesystem call into it’s place by calling [overwriteTargetFileWithTemporary()](https://docs.juce.com/master/classTemporaryFile.html#a21379b8b4737b07594ea0e649f2600ae).

Make sure to respect the warnings in the linked function:

> Before calling this, make sure:
> 
> - that you’ve actually written to the temporary file
> - that you’ve closed any open streams that you were using to write to it
> - and that you don’t have any streams open to the target file, which would prevent it being overwritten

The InterProcessLock is platform dependant and I think often implemented on the filesystem similar to pid files.
