KEEP K.I.S.S.

tk's blog

使用了互斥量访问全局变量还需要 volatile 吗

tk posted @ Jun 25, 2012 07:43:50 PM in C with tags pthread , 2343 阅读

今天在看 pthread ,pthread_cond_wait 是个很有趣的函数,至于标题所提出的问题也是困扰我很久的一个“小问题”。

因为“虽然”互斥量能保证多个线程访问全局变量的一致性,但是由于编译器优化和 cache 机制,多个线程对于内存中变量操作也许就没有“真正”反馈到内存里(也许仅仅改变了寄存器中的缓存值),但是这个说法又与互斥量的目的有矛盾(既然都要保证一致性,为什么又“出现”这种内存中“不一致”的状况?)

于是就找资料,终于找到一个很完备的解释:多处理器环境和线程同步的高级话题(白杨) 涉及到 内存屏障 (memory barrier)技术。

至于 pthread_cond_wait 和 pthread_cond_t 的使用,也是有很多需要理解和注意的细节,在这里可以解惑:
http://www.cppblog.com/izualzhy/archive/2011/11/14/160120.html

pthread 的使用教程这里有:http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html

不过都是英文,可能在一些细节解释上理解会有难度,不过有搜索引擎还是很容易的,另外也可以参考 《Unix 高级环境编程》也就是所谓的“APUE”。

越来越喜欢 C 的风格和思想了,算是回归吧。

  • 无匹配
  • 无匹配
CBSE Class 2 Compute 说:
Sep 14, 2022 01:14:11 PM

CBSE students can download the example practise question bank for the computer examination with suggested solutions for all languages and topics. Based on the new test structure, CBSE STD-2 Computer Sample Paper 2023 Pdf was created. CBSE Class 2 Computer Sample Paper Every student will be able to comprehend the test format thanks to the introduction of the computer exam sample paper for at-home practise. The chapter-wide Computer Sample Paper was created for board students in both mediums by subject specialists from the board and other top educational institutions.

seo service UK 说:
Jan 16, 2024 05:24:44 PM

fantastic points altogether, you just gained emblem reader. What would you suggest in regards to your post that you made a few days ago? Any certain?


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter