• d为何读写不一致


    import std.stdio;
    import core.sync.rwmutex : ReadWriteMutex;
    
    enum PAGE_SIZE = 4096;
    alias PageId = uint;
    
    ubyte[] toBytes(uint value)
    {
        ubyte[] bytes = new ubyte[4];
        bytes[0] = cast(ubyte)(value >> 24);
        bytes[1] = cast(ubyte)(value >> 16);
        bytes[2] = cast(ubyte)(value >> 8);
        bytes[3] = cast(ubyte)(value);
        return bytes;
    }
    
    class DiskManager
    {
        File dbFile;
        __gshared ReadWriteMutex dbIOMutex;
    
        shared static this()
        {
            this.dbIOMutex = new ReadWriteMutex(ReadWriteMutex.Policy.PREFER_WRITERS);
        }
    
        this(string dbFileName)
        {
            this.dbFile = File(dbFileName, "ab+");
        }
    
        void removeIfExists()
        {
            static import std.file;
    
            if (std.file.exists(dbFile.name))
            {
                std.file.remove(dbFile.name);
            }
        }
    
        void readPage(PageId pageId, out ubyte[PAGE_SIZE] pageData)
        {
            synchronized (dbIOMutex.reader)
            {
                dbFile.seek(pageId * PAGE_SIZE);
                dbFile.rawRead(pageData);
            }
        }
    
        void writePage(PageId pageId, ubyte[PAGE_SIZE] pageData)
        {
            synchronized (dbIOMutex.writer)
            {
                dbFile.seek(pageId * PAGE_SIZE);
                dbFile.rawWrite(pageData);
            }
        }
    }
    
    void singleReadWrite()
    {
         // Scenario: 写并读回
        DiskManager diskManager = new DiskManager("test.db");
        diskManager.removeIfExists();
    
        PageId pageId = 0;
    
        ubyte[PAGE_SIZE] pageData = new ubyte[PAGE_SIZE];
        pageData[0..4] = [1,2,3,4];
        diskManager.writePage(pageId, pageData);
    
        ubyte[PAGE_SIZE] readData = new ubyte[PAGE_SIZE];
        diskManager.readPage(pageId, readData);
    
        writeln("pageData[0..4] = ", pageData[0 .. 4]);
        writeln("readData[0..4] = ", readData[0 .. 4]);
    
        assert(pageData[0..4] == readData[0..4]);
    }
    
    void multiReadWrite()
    {
        // 场景: 成功读写相同位置
        DiskManager diskManager = new DiskManager("test.db");
        diskManager.removeIfExists();
    
        PageId pageId = 0;
    
        foreach (i; 0 .. 10)
        {
            ubyte[PAGE_SIZE] pageData = new ubyte[PAGE_SIZE];
    
            pageData[0 .. 4] = toBytes(i);
            diskManager.writePage(pageId, pageData);
    
            ubyte[PAGE_SIZE] readData = new ubyte[PAGE_SIZE];
            diskManager.readPage(pageId, readData);
    
            writeln("pageData[0..4] = ", pageData[0 .. 4]);
            writeln("readData[0..4] = ", readData[0 .. 4]);
    
            assert(pageData[0] == readData[0]);
            assert(pageData[1] == readData[1]);
            assert(pageData[2] == readData[2]);
        }
    }
    
    void main()
    {
    
        singleReadWrite();
        writeln("~~~~~~");
        multiReadWrite();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116

    单次读写正常:

    pageData[0..4] = [1, 2, 3, 4]
    readData[0..4] = [1, 2, 3, 4]
    
    • 1
    • 2

    放在for循环,就出错了:

    pageData[0..4] = [0, 0, 0, 0]
    readData[0..4] = [0, 0, 0, 0]
    
    pageData[0..4] = [0, 0, 0, 1]
    readData[0..4] = [0, 0, 0, 0]
    
    pageData[0..4] = [0, 0, 0, 2]
    readData[0..4] = [0, 0, 0, 0]
    
    pageData[0..4] = [0, 0, 0, 3]
    readData[0..4] = [0, 0, 0, 0]
    
    pageData[0..4] = [0, 0, 0, 4]
    readData[0..4] = [0, 0, 0, 0]
    
    // ...
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    可能,需要刷新.

    void writePage(PageId pageId, ubyte[PAGE_SIZE] pageData)
    {
        synchronized (dbIOMutex.writer)
        {
            dbFile.seek(pageId * PAGE_SIZE);
            dbFile.rawWrite(pageData);
            dbFile.flush();//加了这句.
            dbFile.sync();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    仍然有问题.
    问题出在模式上.

    exists(dbFileName) ? "r+" : "w+"
    
    • 1

    这样,就可修复了.应重新实现FILE*描述符.

  • 相关阅读:
    【多线程】线程安全的单例模式
    Javascript知识【JS变量和运算符】
    nginx下载安装与反向代理及负载均衡应用
    Springboot整合RabittMQ
    红色Cy5 NHS生物相容性与溶解性1263093-76-0
    如何快速水出人生中的第一篇SCI,SCI收割器。
    遭到全网嘲讽,宋丹丹这次被骂惨了...
    企业简化客户服务的5种方法
    多线程系列(十) -ReadWriteLock用法详解
    土豆网鼠标经过显示遮罩---pink老师课程案例
  • 原文地址:https://blog.csdn.net/fqbqrr/article/details/126598005