Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <thread>
- #include <stdio.h>
- #include <memory.h>
- // compile with g++ cachetear.cpp -lpthread -O2 -g -o cachetear
- // spams without optimization
- // 64 byte cache lines, tear should cross the line
- //#pragma pack(0) // for MSVC
- struct __attribute__((packed)) CTest
- {
- char padding[62];
- unsigned int tear;
- };
- int main(void)
- {
- CTest test;
- memset(&test, 0, sizeof(test));
- const int testVal1 = 0xABABABAB;
- const int testVal2 = 0xCDCDCDCD;
- auto test1 = [&test]()
- {
- while(1)
- {
- test.tear = testVal1;
- }
- };
- auto test2 = [&test]()
- {
- while(1)
- {
- test.tear = testVal2;
- }
- };
- std::thread t1(test1);
- std::thread t2(test2);
- printf("Setting up threads...\n");
- while(1)
- {
- if(!(test.tear == testVal1 || test.tear == testVal2))
- {
- printf("Got torn val: 0x%X\n", test.tear);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement