首页 \ 问答 \ 为什么原子操作被认为是线程安全的?(Why are atomic operations considered thread-safe?)

为什么原子操作被认为是线程安全的?(Why are atomic operations considered thread-safe?)

原子操作如何使线程安全? 我已阅读了维基百科有关线程安全的文章 。 但文章并没有真正解释幕后的过程。 换句话说,为什么线程A执行的“原子”操作不能被线程B中断?


How are atomic operations made thread-safe? I've read about the subject in Wikipedia's article on thread-safety. But the article didn't really explain the process behind the scenes. In other words, why can't an "atomic" operation executed by a thread A be interrupted by a thread B?


原文:https://stackoverflow.com/questions/14370575
更新时间:2024-03-01 20:03

最满意答案

这里有一些巧妙的技巧。 考虑这个增强模型:

class Person(db.Model):
  first_name = db.StringProperty()
  last_name = db.StringProperty()
  middle_name = db.StringProperty()
  names_lower = db.StringListProperty()

您需要让names_lower与真实字段保持同步,例如:

p.names_lower = [p.first_name.lower(), p.last_name.lower(),
                 p.middle_name.lower()]

您可以使用DerivedProperty更优雅地完成此操作

现在,您的查询:

term = self.request.get('term').lower()
query = Person.all()
query.filter('names_lower >=', term)
query.filter('names_lower <=', unicode(term) + u"\ufffd")

这给你:

  • 使用一个索引匹配所有3个属性
  • 不区分大小写的匹配项
  • 通配符后缀匹配

因此,对“smi”的查询将返回任何名称以“smi”开头的人。

将较小的名称复制到ListProperty可启用不区分大小写的匹配,并允许我们使用一个查询搜索所有3个字段。 “\ ufffd”是最高可能的unicode字符,因此它是我们子字符串匹配的上限。 如果由于某种原因您想要完全匹配,请过滤'names_lower =', term而不是'names_lower =', term

编辑

我应该如何在我的数据存储区中搜索相同的内容(因为我需要查看每个字段,可能还有3个字段的组合值)? 如何优化这样的查询?

这已经在原始解决方案中得到了解决。 通过获取3个字段并将它们复制到单个ListProperty,我们实际上创建了一个单个索引,每个人有多个条目。 如果我们有一个名叫Bob J Smith的人,我的索引中会有3个点击:

  • names_lower = bob
  • names_lower = j
  • names_lower =史密斯

这消除了在每个字段上运行不同查询的需要。

我也不清楚答复格式应该是什么。

仔细阅读文档 。 格式化jQuery的输出应该非常简单。 您的数据源将是指定URL的字符串,您需要将响应格式化为JSON。


There are a few neat tricks here. Consider this augmented model:

class Person(db.Model):
  first_name = db.StringProperty()
  last_name = db.StringProperty()
  middle_name = db.StringProperty()
  names_lower = db.StringListProperty()

You'll need to keep names_lower in sync with the real fields, e.g.:

p.names_lower = [p.first_name.lower(), p.last_name.lower(),
                 p.middle_name.lower()]

You can do this more elegantly with a DerivedProperty.

And now, your query:

term = self.request.get('term').lower()
query = Person.all()
query.filter('names_lower >=', term)
query.filter('names_lower <=', unicode(term) + u"\ufffd")

This gives you:

  • Matching on all 3 properties with one index
  • Case insensitive matches
  • Wildcard suffix matches

So a query for "smi" will return any person with any name starting with "smi" in any case.

Copying lower-cased names to a ListProperty enables case-insensitive matching, and allows us to search all 3 fields with one query. "\ufffd" is the highest possible unicode character, so it's the upper limit for our substring match. If for some reason you want an exact match, filter for 'names_lower =', term instead.

Edit:

How should I search for the same in my datastore (since I need to look at each field and probably for combined value of 3 fields)? How to optimize such query?

This is already accounted for in the original solution. By taking the 3 fields and copying them to a single ListProperty, we're essentially creating a single index with multiple entries per person. If we have a person named Bob J Smith, he'll have 3 hits in our index:

  • names_lower = bob
  • names_lower = j
  • names_lower = smith

This eliminates the need to run distinct queries on each field.

I am also not clear what should be the reply format.

Read the docs carefully. Formatting output for jQuery should be pretty straightforward. Your data source will be a string specifying a URL, and you'll want to format the response as JSON.

相关问答

更多

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)