知识点整理

   

关于超线程

CPU的超线程简单来讲,就是当一个线程cache miss需要执行访存操作时,访存期间CPU会等待,此时切换到另一个线程执行,避免浪费CPU资源。访存比较慢,超线程的切换设计的很快,号称只要一个CPU周期。

超线程为什么那么快切换,普通线程切换慢因为多个线程使用的东西是一个,比如寄存器,当普通线程切换时,必须将一个线程的值切换成另一个线程的值。而超线程很多东西是独立的(对于软件能看到的东西,对于每一个超线程都是独立的,对于软件看不到的,比如缓存(纯硬件的事情,软件是无法控制的),是否设计的独立,只有CPU设计人知道。),因此超线程切换很快,直接使用另一个超线程的就可以了,不需要寄存器内部数据的切换。

  20140724 追加

  超线程,有时称为同时多线程,是一项允许一个CPU执行多个控制流的技术。它涉及CPU某些硬件有多个备份,比如程序计数器和寄存器;而其他的硬件部分只有一份,比如执行浮点算术运算的单元。常规的处理器需要大约20000个时钟周期做不同线程间的转换,而超线程的处理器可以在单个周期的基础上决定要执行哪一个线程。这使得cpu 能够更好的利用它的硬件资源。例如,假设一个线程必须等到某些数据被装载到高速缓存中,那CPU就可以继续去执行另一个线程。

C语言关键字volatile 

编译器编译代码时会进行优化,根据代码上下文判断,某个变量的值没有变,下次使用就会直接使用寄存器里面的值,而不会访存。

但如果两个进程共享一个内存,A进程改,B进程读,B进程,有可能一直读不到正确的数据,因为他一直使用寄存器里面的值。

此时就需要写代码时,加上volatile关键字,告诉编译器不要优化,直接访存而不是拿寄存器的值。

但访存不一定是真的访问内存,有可能直接从缓存中取值,缓存是软件控制不了的(代码无法指定在内存读还是在缓存读),是硬件控制的。

不要以为加上volatile就会强制从内存里面读,他只是告诉编译器不要优化成读寄存器,而是访存,但访存是访内存还是访缓存,就是运行时硬件的事了。

CPU的发展

All CPUs are connected via a common bus (the Front Side Bus, FSB)to the Northbridge.

In the earliest days of the PC, all communication with devices on eithr bridge had to pass through the CPU, negatively impacting overall system performance.

Today all high performance devices attached to any of the buses can utilize DMA.While this greatly reduces the workload on the CPU, it also creates contention for the bandwidth of the Northbridge as DMA requests compete wieh RAM access from the CPUs.

On some more expensive systems, the Northbridge does not actually contain the memory controller.Instead the Northbridge can be connected to a number of external memory controllers.

One other increasingly popular way is to integrage memory controllers into the CPUs and attach memory to each CPU. This architecture is made popular by SMP systems.

       摘自《what every programmer should know about memory》

原文地址:https://www.cnblogs.com/jintianfree/p/3857506.html