使用动态密钥创建对象[重复](Creating object with dynamic key [duplicate])
这个问题在这里已经有了答案:
- 用动态键创建对象[重复] 2个答案
我必须根据一些动态值创建一个对象键。 这里
str
的值将从用户到用户。 用户输入str的值。 我想使用用户输入的str
值来创建Object。var str = $('#inputValue'); var obj = new Object(); obj.assignment = new Object(); obj.assignment.name= "assignmentName"; obj.assignment.str = new Object();
This question already has an answer here:
I have to create a object key based on some dynamic value . Here the value of
str
will from user to user. The user enters the value of str. I want to usestr
value entered by user to create Object.var str = $('#inputValue'); var obj = new Object(); obj.assignment = new Object(); obj.assignment.name= "assignmentName"; obj.assignment.str = new Object();
原文:https://stackoverflow.com/questions/20519183
最满意答案
要确切地确定实际需要多少个时钟周期是很难的。 相反,如果我们假设它是一个x86处理器(如注释中所述),我将尝试解释实际发生的情况,并且运行时库使用“锁定”指令实现
atomic
- 如果这些假设中的任何一个是假的,那么“所有的赌注都没有“俗话说:执行
atomic
操作时,CPU将首先确保它具有对(可能)缓存值的“独占”访问权限。 这意味着向所有其他CPU发送消息说“我是这个变量的”。 然后,每个其他CPU必须刷新其副本(如果已修改)并将该值标记为“无效”,然后回复“我已完成”。一旦所有处理器都说“我已经完成”,主处理器就可以继续。 此过程,尤其是在具有大量CPU(核心)的大型系统中,可能需要相当长的时间(数百或数千个周期)。
当然,CPU可以通过知道没有其他CPU读取此值,或者没有其他CPU为此值发出写入来优化这一点,在这种情况下,它可以非常快。
你也许可以通过编写一些代码来了解它在[在特定的机器上]有多糟糕,你可以在两个线程中循环,比如1秒,尝试更新和读取相同的原子变量,看看如何它是好还是坏。 然后尝试两个“更新”循环(这样你就可以在两个线程之间进行更新)。
It's quite hard to determine exactly how many clockcycles it actually takes. I will, instead, try to explain what actually happens, if we assume it's an x86 processor (as stated in the comment), and the runtime library implements the
atomic
using "locked" instructions - if any of these assumptions are false, then "all bets are off" as the saying goes:When performing a
atomic
operation, the CPU will first ensure it has "exclusive" access to the (potentially) cached value. This means sending a message to all other CPU's saying "I'm it for this variable". Each of the other CPU's then has to flush its copy (if modified) and mark the value as "invalid", and then reply with "I'm done".Once all processors have said "I'm done", the main processor can continue. This process, especially in a large system with lots of CPU(cores) can take quite some time (hundreds or thousands of cycles).
Of course, the CPU can optimise this by knowing that no other CPU has read this value, or that no other CPU has issued a write for this value, in which case, it can be really quick.
You can perhaps get an idea of how bad this is [on a particular machine] by writing some code where you, in two threads, loop around for, say, 1 second, trying to update and read the same atomic variable, and see how good/bad it is. Then try two "update" loops (so you make updates between two threads).
相关问答
更多-
来自“Olaf Dietsche”的代码 USE ATOMIC real 0m1.958s user 0m1.957s sys 0m0.000s USE VOLATILE real 0m1.966s user 0m1.953s sys 0m0.010s 如果您使用的是GCC SMALLER 4.7 http://gcc.gnu.org/gcc-4.7/changes.html 增加了对指定C ++ 11 / C11内存模型的原子操作的支持。 这些新的__ato ...
-
挂钟时间无关紧要。 但是,您所描述的内容听起来像是写入读取一致性保证: $1.10[intro.multithread]/20 如果原子对象M上的副作用X发生在M的值计算B之前,则评估B应从X或从M的修改顺序中的X之后的副作用Y取其值。 (翻译标准,“值计算”是一个读,“副作用”是一个写) 特别是,如果您的轻松写入和轻松阅读在同一功能的不同语句中,它们通过先前排序的关系连接,因此它们通过先发生关系连接,因此保证成立。 Wall-clock time is irrelevant. However, what ...
-
是。 读取和写入以下数据类型为atomic:bool,char,byte,sbyte,short,ushort,uint,int,float和引用类型。 如C#语言规范中所述 。 编辑:可能也值得了解volatile关键字。 Yes. Reads and writes of the following data types are atomic: bool, char, byte, sbyte, short, ushort, uint, int, float, and reference types. as ...
-
C ++ 11 N3337草案不需要bool方法。 29.5“原子类型” template
struct atomic { [...] } template <> struct atomic { [...] integral fetch_or(integral , memory_order = memory_order_seq_cst) noexcept; [...] } 29.5 / 1: 关于原子特化的操作的语义在29.6中定义。 29.6.3 ... -
要确切地确定实际需要多少个时钟周期是很难的。 相反,如果我们假设它是一个x86处理器(如注释中所述),我将尝试解释实际发生的情况,并且运行时库使用“锁定”指令实现atomic - 如果这些假设中的任何一个是假的,那么“所有的赌注都没有“俗话说: 执行atomic操作时,CPU将首先确保它具有对(可能)缓存值的“独占”访问权限。 这意味着向所有其他CPU发送消息说“我是这个变量的”。 然后,每个其他CPU必须刷新其副本(如果已修改)并将该值标记为“无效”,然后回复“我已完成”。 一旦所有处理器都说“我已经完成 ...
-
不,volatile不能保证位置将被原子地写入或读取,只是编译器无法优化多次读写。 在某些体系结构中,如果对齐正确,处理器将原子级读取或写入,但这不是通用的,甚至无法通过处理器系列来保证。 在可能的地方,原子的内部实现将利用体系结构特征和原子指令修饰符,那么为什么不使用原子,如果你的意思是原子? No, volatile does not guarantee that the location will be written or read atomically, just the the compiler ...
-
is_steady不是一个函数。 它是一个静态成员。 如果不是const你可以改变它。 is_steady isn't a function. It's a static member. If it weren't const you could change it.
-
数据竞赛的大问题是它们是未定义的行为,并不能保证错误的行为。 而且,这与线程的一般不可预测性和x64内存模型的强度相结合,意味着它很难创建可再现的故障。 稍微更可靠的故障模式是优化器发生意想不到的情况,因为您可以观察组件中的情况。 当然,优化器也是非常挑剔的,如果只改变一个代码行,可能会做出完全不同的事情。 以下是我们在代码中遇到的一个示例故障。 代码实现了一种自旋锁,但没有使用原子。 bool operation_done; void thread1() { while (!operation_don ...
-
只需编写operator + = as: inline AtomicVariable& operator+=(AtomicVariable const &rhs) { atomic += rhs.atomic; return *this; } 在文档中: http : //en.cppreference.com/w/cpp/atomic/atomic operator + =是原子的。 您的示例失败,因为下面的执行方案是可能的: Thread1 - rhs. ...
-
正如在评论中所说,你的问题是变量在你加载它和你增加它的时间之间被另一个线程改变了。 你可以改变你的循环,例如像这样来修复它: while (true) { auto current = sum.load(); if (current >= LIMIT) { return; } auto next = current + 1; sum.compare_exchange_strong(current, next)); } As it wa ...