欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

线程与共享的麻烦事.

程序员文章站 2022-03-11 16:29:24
import std;class S { SysTime a; shared SysTime b; synchronized shared void setIt(SysTime t) { // What I used to do cast () a = t; // Here you need to cast away shared anyway cast () b = t; // so it doesn't make any di...
import std;

class S {
    SysTime a;
    shared SysTime b;

    synchronized shared void setIt(SysTime t) {
        // What I used to do
        cast () a = t; // Here you need to cast away shared anyway
        cast () b = t; // so it doesn't make any difference.

        // What I'll do from now on
        with(cast() this) { // You only notice the difference when you cast away `shared` from `this`
            a = t;
            // b = t; // FAILS
        }
    }
}

Arafel:
`有时您想封装"共享"逻辑.
例如,类(构)可能是跨多线程中存储共享数据线程安全容器,每个线程将获得该容器的共享引用,由该类在内部管理所有同步.
这样,我只将整个容器类标记为"共享",而不必标记每个方法,实际上没有非共享方法.
现在我知道,不应该共享成员,因为它仅在从`本`丢弃共享时显示.
如仅实例化共享变量,则所有成员(包括非共享)也将自动共享.以前,我只从单个成员中删除共享,所以未注意到
`

本文地址:https://blog.csdn.net/fqbqrr/article/details/107385562

相关标签: 笔记