首页 \ 问答 \ 如果只有一个CPU核心,每个线程的内存可见性问题是否存在?(Are memory visibility problems per thread also there if there is just one CPU core?)

如果只有一个CPU核心,每个线程的内存可见性问题是否存在?(Are memory visibility problems per thread also there if there is just one CPU core?)

标题可能看起来很奇怪,所以让我解释一下。 通常关于竞争条件的人说thread1可以看到x == 0而线程2已经x=1;

我的问题是关于在同一个核心上安排的线程(不是不切实际的,仍然有1个核心嵌入式系统,并且至少在理论上你可以将线程绑定到核心):
即使thread1和thread2在同一个核心(X86,ARM)上顺序运行(一个/另一个(使用其他线程)),也会发生通常的竞争可见性问题......


Title might seem weird, so let me explain. Often people teaching about race conditions say thread1 can see x == 0 while thread 2 already did x=1;

My questions is about threads scheduled on same core(not unrealistic, there are still 1 core embeded systems and afaik you could prob at least in theory bind threads to cores) :
Do usual race visibility problems can occur even if thread1 and thread2 run sequentially(one befor/after another( with other threads ofc)) on the same core(X86, ARM)...


原文:https://stackoverflow.com/questions/20761506
更新时间:2023-02-22 15:02

最满意答案

您的第二个“解决方法”是首选方式:

class Outer {
    constructor(public inner: Outer.Inner) { }
}

namespace Outer {
    export class Inner {
        inInner: number
    };
}

// Spec
let outer1 = new Outer({ inInner: 3 });

let outerInner = new Outer.Inner();
let outer2 = new Outer(outerInner);

TypeScript中有三种类型的声明:名称空间,类型和值。 您将使用namespace来组织类型和值。

http://www.typescriptlang.org/docs/handbook/declaration-merging.html

它不会降低可读性,因为TypeScript中没有“内部类”的概念。

请记住, class语法只是es5原型继承的糖。 你如何代表原型继承的内在阶级? 你把is作为实例变量吗? 或者在Outer.prototype.???Outer.prototype.???

它最终在es5中是这样的:

var Outer = function Outer(...) { ... }
Outer.Inner = function Inner() { ... }

如果你看一下, Outer.InnerOuter只作为一个“名称空间”来保存Inner类。


Your second "workaround" is the prefer way to go:

class Outer {
    constructor(public inner: Outer.Inner) { }
}

namespace Outer {
    export class Inner {
        inInner: number
    };
}

// Spec
let outer1 = new Outer({ inInner: 3 });

let outerInner = new Outer.Inner();
let outer2 = new Outer(outerInner);

There are three types of declarations in TypeScript: namespace, type, and value. You would use namespace to organize types and value.

http://www.typescriptlang.org/docs/handbook/declaration-merging.html

It does not reduce readability because there is no concept of an "Inner Class" in TypeScript.

Remember that the class syntax is just sugar of es5 prototypal inheritance. How would you represent an inner class in prototypal inheritance? Do you put is as an instance variable? or under Outer.prototype.???

what it ends up in es5 is something like this:

var Outer = function Outer(...) { ... }
Outer.Inner = function Inner() { ... }

If you look at it, the Outer in Outer.Inner is only served as a "namespace" to hold the Inner class.

相关问答

更多
  • 答案非常简单,类型是Date : const d: Date = new Date(); // but the type can also be inferred from "new Date()" already 它与其他所有对象实例相同:) The answer is super simple, the type is Date: const d: Date = new Date(); // but the type can also be inferred from "new Date()" alr ...
  • 您的静态stores类型不正确。 它应该是Array<{ [x: string]: Store }> (或{ [x: string]: Store }[]但我更喜欢第一个,因为它更易读)。 编辑:如果你想索引商店,它应该是: export class Stores { store: Store; static stores = { '0750078': new Store('0750078', 'Kiosque de Paris', 'Place Colette 75001 Paris', ...
  • 我立即想到将“内部类”(让我们称之为内部)作为外部伴侣对象的成员。 是的,这是最接近的Scala等价物。 但这导致了一个问题:内在的类型不是外#内 这不是问题,因为Outer#Inner相当于Java中的非静态内部类。 即它有一个Outer对象的引用。 我想得到一个不依赖于路径的内部类,或者至少知道这是否可能 如果要创建一个不能以路径依赖方式使用的非伴随内部类,则无法实现。 当然,您可以自由地在代码中编写Outer#Inner而不是o.Inner 。 I immediatelly thought of ma ...
  • 静态内部类是嵌套在另一个具有static修饰符的类中的类。 它与顶级类几乎完全相同,只不过它可以访问其内部定义的类的私有成员。 class Outer { static class Inner1 { } class Inner2 { } } 类Inner1是静态内部类。 类Inner2是一个非静态的内部类。 两者之间的区别在于非静态内部类的实例永久地附加到外部实例 - 如果没有Outer ,则无法创建Inner2 。 但是,您可以单独创建Inner1对象。 A static ...
  • 你可以使用一个箭头函数来保存这个上下文: MsgBox.getInput({ context: this, okCallback: (value) => { this.accessToEnclosingClass } } 编译器(和IDE)也将理解这一点,因此不会将this视为任何。 You can just use an arrow function which saves the context of this: MsgBox.getInput({ ...
  • 那么,使用泛型: function callM(arr: T[]): void { arr.forEach(t => t.m()); } 现在你可以这样做: callM([A, B, C]); // OK callM([A, B, string]); // Error 如果你想存储这些值: class Container { public klazzes: (typeof A)[]; } const cont: Container = new Co ...
  • 您的第二个“解决方法”是首选方式: class Outer { constructor(public inner: Outer.Inner) { } } namespace Outer { export class Inner { inInner: number }; } // Spec let outer1 = new Outer({ inInner: 3 }); let outerInner = new Outer.Inner(); let outer2 = ...
  • 枚举器,函数和对象的名称隐藏了在同一范围内声明的枚举和类的名称。 在您的情况下,数据成员名称隐藏结构的名称。 您可以通过特殊查找访问隐藏的类型名称: 通过忽略对象,函数和枚举器名称来查找::之前的名称。 用于指定基类的名称会忽略任何非类型名称。 精心设计的类型说明符中指定的名称忽略了对象,函数和枚举器名称。 因此,以下详细说明的类型说明符是有效的并且引用该类 struct mySequence::zeroth var; 另请注意,在类范围内,成员声明更改了该声明中使用的名称的含义时,它是不正确的。 在你的 ...
  • 在你的具体情况下,这并不重要。 但 如果你有严格标志(或者扩展名为noImplicitAny标志),它会为第一个,而不是第二个引发错误。 因为不允许隐含任何内容。 他们之所以相同的原因是因为TypeScript无法推断参数的类型或返回类型,所以它们都被推断为任何类型。 任何东西都是你从TypeScript中逃脱出来的,它的意思是“我不知道这是什么类型,所以我用它做的任何事情都是可以接受的,它的任何属性都隐含地存在(并且也有一种类型),我可以称之为有任何参数,我也可以new它。 建议您最小化对隐式或显式any ...
  • ctrl + mouse hover显示Intellij 2016.3中的类型。 ctrl + mouse hover shows the type in Intellij 2016.3.

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。