dAA线安包装器

原文

synchronized final class SyncAA(K, V) ///
{
    ///
    V opIndex(K key) { return data_[key]; }

    ///
    V opIndexAssign(V value, K key) { return data_[key] = value; }

    ///
    K[] keys() const { return data_.keys; }

    ///
    void remove(K key) { data_.remove(key); }

    /// 无`in`符号,非线安.
    V get(K key, lazy V defaultValue=V.init)
    {
        auto p = key in data_;
        return p ? *p : defaultValue;
    }

    ///
    int opApply(scope int delegate(inout ref V) dg) const
    {
        int result = 0;
        foreach (value; data_) {
            result = dg(value);
            if (result)
                break;
        }
        return result;
    }

    ///
    int opApply(scope int delegate(K, inout ref V) dg) const
    {
        int result = 0;
        foreach (key, value; data_) {
            result = dg(key, value);
            if (result)
                break;
        }
        return result;
    }

private:
    V[K] data_;
}

另一个文件:

__gshared serverListCache = new SyncAA!(string, ServerList);

我在每个方法中加上synchronized (this)来修复,但不干净.

我最终按__gshared声明引用,shared/synchronized好像有分歧,而没完成.而__gshared管用.
参考1
参考2

class SyncTable(KEY, VAL) {
    private VAL[KEY] table;
    auto opIndexAssign(VAL value, KEY key) {
        synchronized(this) {
            return table[key] = value;
        }
    }
    int opApply(int delegate(ref KEY, ref VAL) dg) {
        synchronized(this) {
            foreach (key, val; table) {
                if (dg(key, val)) return 1;
            }
            return 0;
        }
    }
    auto opBinaryRight(string op)(KEY key) if (op == "in") {
        synchronized(this) {
            return key in table;
        }
    }
    auto opDispatch(string s, SA...)(SA sargs) {
        synchronized(this) {
            static if (SA.length == 0) {
                mixin(format("return table.%s;", s));
            } else {
                mixin(format("return table.%s(%s);", s, sargs.stringof[6 .. $-1])); // tuple(_param_0)
            }
        }
    }
}

类上有同步,必须:

synchronized class SyncTable(KEY, VAL) {
    // 修改,则必须重新用`未共享`来指定
    auto opIndexAssign(VAL value, KEY key) {
        auto unshared = cast(T) table;
        unshared[key] = value;
        table = cast(shared) unshared;
        return value;
    }
    auto require(KEY key) {
        auto unshared = cast(T) table;
        auto r = unshared.require(key);
        table = cast(shared) unshared;
        return r;
    }
    /* ... */
}

感觉不对,对无同步互斥锁,还有

struct Lock {
    private shared Mutex _mtx;
    this(shared Mutex mtx) {
        _mtx = mtx;
        _mtx.lock();
    }
    this(this) @disable;
    ~this() {
        if (_mtx)
            _mtx.unlock();
        _mtx = null;
    }
}
class SyncTable(KEY, VAL) {
    private VAL[KEY] table;
    shared Mutex mtx;
    this() {
        mtx = new shared Mutex;
    }
    auto opIndexAssign(VAL value, KEY key) {
        auto lock = Lock(mtx);
        return table[key] = value;
    }
    /* ... */
}

试试:

synchronized final class SyncAA(K, V)
{
    V opIndex(K key) { return sharedTable[key]; }
    V opIndexAssign(V value, K key) { return sharedTable[key]=value; }
    const(K[]) keys() const { return unsharedTable.keys; }
    void remove(K key) { sharedTable.remove(key); }
    V get(K key, lazy V defaultValue=V.init)
    {
        auto p = key in sharedTable;
        return p ? *p : defaultValue;
    }
private:
    V[K] sharedTable;
    ref inout(V[K]) unsharedTable() inout
    {
        return *cast(inout(V[K])*)&sharedTable;
    }
}
void f(shared SyncAA!(string,string) a)
{
    a.keys();
    a["12"]="34";
    a.remove("12");
}

带分配:

synchronized final class SyncAA(K, V)
{
    V opIndex(K key) { return sharedTable[key]; }
    V opIndexAssign(V value, K key) { return sharedTable[key]=value; }
    const(K[]) keys() const { return unsharedTable.keys; }
    void remove(K key) { sharedTable.remove(key); }
    V get(K key, lazy V defaultValue=V.init)
    {
        auto p = key in sharedTable;
        return p ? *p : defaultValue;
    }
private:
    V[K] sharedTable;
    ref inout(V[K]) unsharedTable() inout
    {
        return *cast(inout(V[K])*)&sharedTable;
    }
}
shared SyncAA!(string,string) saa;
void f()
{
    saa=new shared SyncAA!(string,string);
    saa.keys();
    saa["12"]="34";
    saa.remove("12");
}

可如下:

synchronized final class SyncAA(K, V)
{
    this(K key, V val) { sharedTable[key]=val; }
    V opIndex(K key) { return sharedTable[key]; }
    V opIndexAssign(V value, K key) { return sharedTable[key]=value; }
    const(K[]) keys() const { return unsharedTable.keys; }
    void remove(K key) { sharedTable.remove(key); }
    V get(K key, lazy V defaultValue=V.init)
    {
        auto p = key in sharedTable;
        return p ? *p : defaultValue;
    }
private:
    V[K] sharedTable;
    inout(V[K]) unsharedTable() inout
    {
        return cast(inout(V[K]))sharedTable;
    }
}
shared SyncAA!(string,string) saa;
void f()
{
    saa=new shared SyncAA!(string,string)("1","2");
    saa.keys();
    saa["12"]="34";
    saa.remove("12");
}
posted @   zjh6  阅读(23)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示