首页 \ 问答 \ 使用动态密钥创建对象[重复](Creating object with dynamic key [duplicate])

使用动态密钥创建对象[重复](Creating object with dynamic key [duplicate])

这个问题在这里已经有了答案:

我必须根据一些动态值创建一个对象键。 这里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 use str 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
更新时间:2023-11-12 12:11

最满意答案

要确切地确定实际需要多少个时钟周期是很难的。 相反,如果我们假设它是一个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 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)