- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在查看 The Open Group Base Specifications Issue 7 (IEEE Std 1003.1, 2013 Edition), section 4.11 的第 4.11 节文档,其中详细说明了内存同步规则。这是我为详细说明 POSIX/C 内存模型而设法获得的 POSIX 标准中最具体的。
这是一段话
4.11 Memory Synchronization
Applications shall ensure that access to any memory location by more than one thread of control (threads or processes) is restricted such that no thread of control can read or modify a memory location while another thread of control may be modifying it. Such access is restricted using functions that synchronize thread execution and also synchronize memory with respect to other threads. The following functions synchronize memory with respect to other threads:
fork() pthread_barrier_wait() pthread_cond_broadcast() pthread_cond_signal() pthread_cond_timedwait() pthread_cond_wait() pthread_create() pthread_join() pthread_mutex_lock() pthread_mutex_timedlock()
pthread_mutex_trylock() pthread_mutex_unlock() pthread_spin_lock() pthread_spin_trylock() pthread_spin_unlock() pthread_rwlock_rdlock() pthread_rwlock_timedrdlock() pthread_rwlock_timedwrlock() pthread_rwlock_tryrdlock() pthread_rwlock_trywrlock()
pthread_rwlock_unlock() pthread_rwlock_wrlock() sem_post() sem_timedwait() sem_trywait() sem_wait() semctl() semop() wait() waitpid()
(省略要求的异常(exception)情况)。
基本上,解释上述文档,规则是当应用程序读取或修改内存位置而另一个线程或进程可能修改它时,它们应该确保同步线程执行和内存 相对于其他线程 通过调用列出的函数之一。其中,pthread_create(3)
提到提供内存同步。
我知道这基本上意味着需要某种 memory barrier每个功能都隐含(尽管标准似乎不使用该概念)。因此,例如从 pthread_create()
返回,我们可以保证该线程在调用之前所做的内存修改在其他线程(运行可能不同的 CPU/内核)也同步内存之后出现。但是新创建的线程呢 - 在线程开始运行线程函数之前是否存在隐含的内存屏障,以便它始终看到由 pthread_create()
同步的内存修改?这是标准规定的吗?或者我们是否应该显式提供内存同步,以便能够根据 POSIX 标准信任我们读取的任何数据的正确性?
特殊情况(作为特殊情况回答上述问题):上下文切换是否提供内存同步,即,当进程或线程的执行开始或恢复时,内存是否相对于任何同步其他执行线程的内存同步?
示例:
Thread #1
创建一个从堆中分配的常量对象。线程 #1
创建一个新线程 #2
从对象中读取数据。如果我们可以假设新线程 #2
以内存同步开始,那么一切都很好。但是,如果运行新线程的 CPU 核心具有先前分配的副本,但由于在其高速缓存内存中丢弃了数据而不是新值,那么它可能有错误的状态 View ,并且应用程序可能无法正常运行。
更具体...
之前在程序中(这是CPU#1缓存中的值)
int i = 0;
线程 T0
在 CPU #0 中运行:
pthread_mutex_lock(...);
int tmp = i;
pthread_mutex_unlock(...);
在 CPU #1 中运行的线程 T1
:
i = 42;
pthread_create(...);
新创建的线程 T2
在 CPU #0 中运行:
printf("i=%d\n", i); /* First step in the thread function */
没有内存屏障,没有同步线程 T2
内存,输出可能会是
i=0
(以前缓存的,未同步的值)。
更新:如果允许这种疯狂的实现,许多使用 POSIX 线程库的应用程序将不是线程安全的。
最佳答案
is there implied memory barrier before the thread starts running the thread function so that it unfailingly sees the memory modifications synchronized by pthread_create()?
是的。否则,pthread_create 就没有意义充当内存同步(屏障)。
(这是 afaik。posix 没有明确说明,(nor does posix define a standard memory model),所以你必须决定你是否相信你的实现会做它可能做的唯一理智的事情——确保在新线程运行之前同步——我不会特别担心它)。
Special case (which would as a special case answer the above question): does a context switch provide memory synchronization, that is, when the execution of a process or thread is started or resumed, is the memory synchronized with respect to any memory synchronization by other threads of execution?
不,上下文切换不作为障碍。
Thread #1 creates a constant object allocated from heap. Thread #1 creates a new thread #2 that reads the data from the object. If we can assume the new thread #2 starts with memory synchronized then everything is fine. However, if the CPU core running the new thread has copy of previously allocated but since discarded data in its cache memory instead of the new value, then it might have wrong view of the state and the application may function incorrectly.
由于pthread_create必须进行内存同步,所以不能发生这种情况。驻留在另一个核心上的 cpu 缓存中的任何旧内存都必须失效。 (幸运的是,常用的平台是缓存一致的,所以硬件会负责)。
现在,如果您在创建 2. 线程后更改对象,则需要再次进行内存同步,以便所有各方都能看到更改,否则会避免竞争条件。 pthread 互斥量通常用于实现这一点。
关于c - SMP 架构中的 pthread_create(3) 和内存同步保证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22347856/
是否可以简化在裸机上运行的这条链: 具有随时间变化的副本数的 StatefulSet 服务 使用 proxy-next-upstream: "error http_502 timeout invali
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我需要为应用程序制定架构。它专为销售产品而设计。 系统每天将接受大约 30-40k 的新产品。它将导致在表 product 中创建新记录。 系统应保留价格历史记录。用户应该能够看到产品 A 的价格在去
我需要一些帮助来理解 PHP 的内部工作原理。 还记得,在过去,我们曾经写过 TSR(Terminate and stay resident)例程(pre-windows 时代)吗?一旦该程序被执行,
1.Nginx 基础架构 nginx 启动后以 daemon 形式在后台运行,后台进程包含一个 master 进程和多个 worker 进程。如下图所示: master与
本文深入探讨了Kubernetes(K8s)的关键方面,包括其架构、容器编排、网络与存储管理、安全与合规、高可用性、灾难恢复以及监控与日志系统。 关注【TechLeadCloud】,
我知道 CNN 的工作原理,包括每一层的用途(Dropout、Pooling 等)。但是,在为新数据集设计 CNN 时,我不知道要使用多少个 Conv-Relu-Pool 层,在最终获得输出之前我应该
在基于 REST 的架构中,资源和方法之间有什么区别。有吗? 最佳答案 资源是您的应用程序定义的东西;它们与物体非常相似。方法是 HTTP 动词之一,例如 GET、POST、PUT、DELETE。它们
我想用 oneOf仅在 xyType 的值上不同的模式属性(property)。我想要其中两个:一个是 xyType设置为 "1"第二个在哪里xyType是 任何其他值 .这可以使用 json 模式完
寻求 PHP 架构师的建议! 我对 PHP 不是很熟悉,但已经接管了一个用该语言编写的大型分析包的维护工作。该架构旨在将报告的数据读取到大型键/值数组中,这些数组通过各种解析模块传递,以提取每个模块已
这些存在吗? 多年来,我一直是大型强类型面向对象语言(Java 和 C#)的奴隶,并且是 Martin Fowler 及其同类的信徒。 Javascript,由于它的松散类型和函数性质,似乎不适合我习
我已经阅读了 Manning 的 Big Data Lambda Architecture ( http://www.manning.com/marz/BD_meap_ch01.pdf ),但仍然无法
在过去的几年里,我做了相当多的 iOS 开发,所以我非常熟悉 iOS 架构和应用程序设计(一切都是一个 ViewController,您可以将其推送、弹出或粘贴到选项卡栏中)。我最近开始探索正确的 M
我有以下应用程序,我在其中循环一些数据并显示它。 {{thing.title}} {{thing.description}}
昨天我和我的伙伴讨论了我正在开发的这个电子购物网站的架构。请注意,我为此使用 ASP.NET。他非常惊讶地发现我没有将添加到购物车的项目保留在 ArrayList 或其他通用列表中,而是使用 LINQ
我正在使用在 tridion 蓝图层次结构中处于较低位置的出版物。从蓝图中较高级别的出版物继承的一些内容和模式不适合我的出版物,并且永远不会被我的出版物使用。 我将跟进添加这些项目的内部团队,并尝试说
我目前已经在 Cassandra 中设计了一个架构,但我想知道是否有更好的方法来做事情。基本上,问题在于大多数(如果不是全部)读取都是动态的。我构建了一个分段系统作为应用程序服务,读取动态自定义查询(
我正在按照 documentation 中给出的 icingaweb UI v 2.0 布局执行在服务器上设置 icinga 的步骤。 。我成功进入设置页面,该页面要求您输入 token ,然后按照步
我必须保存来自不同社交媒体的用户的不同个人资料。例如用户可能有 1 个 Facebook 和 2 个 Twitter 个人资料。如果我保存每个配置文件它作为新文档插入不同的集合中,例如 faceboo
我的团队使用 Puppet 架构,该架构目前可在多个环境(流浪者、暂存、生产)中容纳单个应用程序。 我们现在想要扩展此设置的范围以支持其他应用程序。他们中的许多人将使用我们已经定义的现有模块的子集,而
我是一名优秀的程序员,十分优秀!