Disk write in a "for" loop with RwMutex never happens

ag0aep6g anonymous at example.com
Mon Aug 29 16:21:53 UTC 2022


On Sunday, 28 August 2022 at 22:46:17 UTC, Gavin Ray wrote:
> I've put the code, stripped to a minimal example here:
> - https://ldc.godbolt.org/z/fzsx3Tnnn
[...]
>
> But if the same code is placed inside of a `for` loop, suddenly 
> no writes occur:
[...]
>
> Does anyone know what is happening here? It's really puzzling.

Relevant pieces of the code:

```d
class DiskManager
{
     void writePage(PageId pageId, ubyte[PAGE_SIZE] pageData)
     {
         synchronized (dbIOMutex.writer)
         {
             dbFile.seek(pageId * PAGE_SIZE);
             dbFile.rawWrite(pageData);
         }
     }
}

void singleReadWrite()
{
     PageId pageId = 0;

     diskManager.writePage(pageId, pageData);
}

void multiReadWrite()
{
     PageId pageId = 0;

     foreach (i; 0 .. 10)
     {
         diskManager.writePage(pageId, pageData);
     }
}
```

You never change `pageId`. So as far as I can tell, you're always 
`seek`-ing to the same position, and you just overwrite the same 
piece of the file again and again.


More information about the Digitalmars-d-learn mailing list