浅谈 Cache
先来看一段代码:
#include <Windows.h> #include <stdio.h> int x, y, r1, r2; HANDLE Event1, Event2, EventMain1, EventMain2; DWORD _stdcall ThreadProc1 ( LPVOID lpThreadParameter ) { while (TRUE) { WaitForSingleObject(Event1, INFINITE); while (rand() % 8 != 0); x = 1; r1 = y; SetEvent(EventMain1); } } DWORD _stdcall ThreadProc2 ( LPVOID lpThreadParameter ) { while (TRUE) { WaitForSingleObject(Event2, INFINITE); while (rand() % 8 != 0); y = 1; r2 = x; SetEvent(EventMain2); } } void main() { HANDLE Thread1, Thread2; Thread1 = CreateThread(NULL, 0, ThreadProc1, NULL, 0, NULL); Thread2 = CreateThread(NULL, 0, ThreadProc2, NULL, 0, NULL); SetThreadAffinityMask(Thread1, 1); SetThreadAffinityMask(Thread2, 2); Event1 = CreateEvent(NULL, FALSE, FALSE, NULL); Event2 = CreateEvent(NULL, FALSE, FALSE, NULL); EventMain1 = CreateEvent(NULL, FALSE, FALSE, NULL); EventMain2 = CreateEvent(NULL, FALSE, FALSE, NULL); int dectected = 0; for (int i = 0; ;i++) { x = 0; y = 0; SetEvent(Event1); SetEvent(Event2); WaitForSingleObject(EventMain1, INFINITE); WaitForSingleObject(EventMain2, INFINITE); if (r1 == 0 && r2 == 0) { printf("detected %d, i %d\n", dectected++, i); } } }
描述一下这段代码的重点,起两个线程在两个cpu 核心上执行,分别执行 x = 1, r1= y; y = 1, r2 = x;无论哪个线程先执行,或一起执行,按照常理执行结果一般为 r1 = 1 或 r2 = 1 或者两个都为 1,也就是在 main 函数中的判断 r1 和 r2 同为 0 的判断是不可能成立的,打印也不会出现。
当你真实在一台至少双核机器上运行时,你应该不会相信你的眼睛。
detected 305, i 202654 detected 306, i 204704 detected 307, i 205029 detected 308, i 206454 detected 309, i 207135 detected 310, i 208178 detected 311, i 208610 detected 312, i 208879 detected 313, i 208896 detected 314, i 208960 detected 315, i 209003 detected 316, i 209869 detected 317, i 210573 detected 318, i 211259 detected 319, i 211275 detected 320, i 211487 detected 321, i 212646 detected 322, i 212963 detected 323, i 213317 detected 324, i 213382 detected 325, i 213473 detected 326, i 213588 detected 327, i 214940 detected 328, i 215055 detected 329, i 215271(由于贴图不方便,直接把本机的打印复制下来了)
从打印中可以得出,在两个线程运行完后,r1== r2 == 0 的情况居然出现了,难道 CPU 在逗我们么。
CPU 没有逗我们,是我们不够了解它。(待续)
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。