#include <windows.h>
#include <thread>
enum {Count = 40000000};
union
{
volatile long valueA;
char _1[64];
};
union
{
volatile long valueB;
char _2[64];
};
union
{
volatile long valueC;
char _3[64];
};
for (int j = 0; j < 10; ++j)
{
if (j == 3) //第3+1輪開啓一個讀線程讀A
{
puts("以下開啓線程 read A-------------------");
std::thread
([pA = &valueA, pB = &valueB, pC = &valueC]()
{
volatile long v;
while (true)
{
v = *pA;
for (int i = 0; i < 1; ++i)
_mm_pause();
Sleep(0);
}
}).detach();
}
else if (j == 6) //第6+1輪再開啓一個讀線程讀B
{
puts("以下開啓線程 read B-------------------");
std::thread
([pA = &valueA, pB = &valueB, pC = &valueC]()
{
volatile long v;
while (true)
{
v = *pB;
for (int i = 0; i < 1; ++i)
_mm_pause();
Sleep(0);
}
}).detach();
}
{
uint32_t t = GetTickCount();
for (int i = 0; i < Count; ++i)
{
InterlockedIncrement(&valueA);
}
t = GetTickCount() - t;
printf("同一個變量 {incA} = %dM組/s, *1=%dM/s\n", Count / t / 1000, 1 * Count / t / 1000);
}
{
uint32_t t = GetTickCount();
for (int i = 0; i < Count; ++i)
{
InterlockedIncrement(&valueA);
InterlockedDecrement(&valueA);
}
t = GetTickCount() - t;
printf("同一個變量 {incA, decA} = %dM組/s, *2=%dM/s\n", Count / t / 1000, 2 * Count / t / 1000);
}
{
uint32_t t = GetTickCount();
for (int i = 0; i < Count; ++i)
{
InterlockedIncrement(&valueA);
InterlockedIncrement(&valueB);
}
t = GetTickCount() - t;
printf("不同cacheline變量 {incA, incB } = %dM組/s, *2=%dM/s\n", Count / t / 1000, 2 * Count / t / 1000);
}
{
uint32_t t = GetTickCount();
for (int i = 0; i < Count; ++i)
{
InterlockedIncrement(&valueA);
InterlockedIncrement(&valueB);
InterlockedIncrement(&valueC);
}
t = GetTickCount() - t;
printf("不同cacheline變量 {incA, incB, incC } = %dM組/s, *3=%dM/s\n", Count / t / 1000, 3 * Count / t / 1000);
}
puts("");
}
同一個變量 {incA} = 150M組/s, *1=150M/s
同一個變量 {incA, decA} = 75M組/s, *2=150M/s
不同cacheline變量 {incA, incB } = 88M組/s, *2=176M/s
不同cacheline變量 {incA, incB, incC } = 49M組/s, *3=147M/s
同一個變量 {incA} = 160M組/s, *1=160M/s
同一個變量 {incA, decA} = 77M組/s, *2=155M/s
不同cacheline變量 {incA, incB } = 88M組/s, *2=176M/s
不同cacheline變量 {incA, incB, incC } = 49M組/s, *3=147M/s
同一個變量 {incA} = 160M組/s, *1=160M/s
同一個變量 {incA, decA} = 77M組/s, *2=155M/s
不同cacheline變量 {incA, incB } = 91M組/s, *2=183M/s
不同cacheline變量 {incA, incB, incC } = 49M組/s, *3=147M/s
以下開啓線程 read A-------------------
同一個變量 {incA} = 121M組/s, *1=121M/s
同一個變量 {incA, decA} = 64M組/s, *2=128M/s
不同cacheline變量 {incA, incB } = 71M組/s, *2=142M/s
不同cacheline變量 {incA, incB, incC } = 39M組/s, *3=118M/s
同一個變量 {incA} = 128M組/s, *1=128M/s
同一個變量 {incA, decA} = 61M組/s, *2=122M/s
不同cacheline變量 {incA, incB } = 71M組/s, *2=142M/s
不同cacheline變量 {incA, incB, incC } = 41M組/s, *3=124M/s
同一個變量 {incA} = 121M組/s, *1=121M/s
同一個變量 {incA, decA} = 61M組/s, *2=122M/s
不同cacheline變量 {incA, incB } = 73M組/s, *2=146M/s
不同cacheline變量 {incA, incB, incC } = 41M組/s, *3=124M/s
以下開啓線程 read B-------------------
同一個變量 {incA} = 121M組/s, *1=121M/s
同一個變量 {incA, decA} = 64M組/s, *2=128M/s
不同cacheline變量 {incA, incB } = 64M組/s, *2=128M/s
不同cacheline變量 {incA, incB, incC } = 37M組/s, *3=111M/s
同一個變量 {incA} = 128M組/s, *1=128M/s
同一個變量 {incA, decA} = 62M組/s, *2=125M/s
不同cacheline變量 {incA, incB } = 64M組/s, *2=128M/s
不同cacheline變量 {incA, incB, incC } = 36M組/s, *3=108M/s
同一個變量 {incA} = 121M組/s, *1=121M/s
同一個變量 {incA, decA} = 64M組/s, *2=128M/s
不同cacheline變量 {incA, incB } = 64M組/s, *2=128M/s
不同cacheline變量 {incA, incB, incC } = 37M組/s, *3=111M/s
同一個變量 {incA} = 121M組/s, *1=121M/s
同一個變量 {incA, decA} = 62M組/s, *2=125M/s
不同cacheline變量 {incA, incB } = 64M組/s, *2=128M/s
不同cacheline變量 {incA, incB, incC } = 34M組/s, *3=103M/s
此外,此測試進程,開兩份時與一份幾乎無差異,開三份時數字也有一份的2/3以上。