如果只有一个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 didx=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
最满意答案
您的第二个“解决方法”是首选方式:
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.Inner
的Outer
只作为一个“名称空间”来保存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 underOuter.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
inOuter.Inner
is only served as a "namespace" to hold theInner
class.
相关问答
更多-
打字稿日期类型?(Typescript Date Type?)[2024-05-10]
答案非常简单,类型是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 ... -
如何编写索引集合作为打字稿中的静态属性?(How to write indexed collection as a static property in typescript?)[2021-08-07]
您的静态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.