CPU Cache伪共享(False Sharing)

出现伪共享原因:多个线程同时读写同一个 Cache Line 的不同变量时,而导致 CPU Cache 失效的现象称为伪共享(False Sharing)

查看系统Cache Line加载数据大小

cat /sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size 
64

解决方式:
结构体对齐Cache Line大小(一般为64 bytes)
避免的方式一般有 Cache Line 大小字节对齐,以及字节填充等方法

#include <iostream>
#include <thread>
#include <chrono>


using namespace std;
using namespace std::chrono;

#define ____cacheline_aligned __attribute__((__aligned__(64)))
#define LOOP 10000 * 10000
struct data {   // 4 bytes
    int32_t x ;
}/*____cacheline_aligned*/;
 
struct data2 {  // 64 bytes
    int32_t x ____cacheline_aligned;
} ;

struct data3 {      // 128 bytes
    int32_t y ;
    int32_t x ____cacheline_aligned;    
} ;

typedef struct data3 Data;
 
Data dArray[2];
 
void f1() {
    int64_t i = 0;
    for(i = 0; i < LOOP; i++) {
        dArray[0].x = 2;
    }
   
    printf("f1 complete\n");
}
 
void f2() {
    int64_t i = 0;
    for(i= 0; i < LOOP; i++) {
        dArray[1].x = 1;
    }
    printf("f2 complete\n");
}
 
int main() {
    printf("sizeof(Data):%ld\n", sizeof(Data)); 
    auto time1 = steady_clock::now();

    std::thread t1(f1);
    std::thread t2(f2);
    t1.join();
    t2.join();
    auto time2 = steady_clock::now();
    auto ts = duration_cast<chrono::milliseconds>(time2-time1).count();
    std::cout << "f1 time:"<<ts<<" ms"<<std::endl;
    printf("complete\n");
    return 0;
}



posted @ 2023-03-16 11:32  小小灰迪  阅读(120)  评论(0)    收藏  举报