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");
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现