Created
January 15, 2025 20:24
-
-
Save alexcrichton/71784e5b9058d31c858ac33bb364a081 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <thread> | |
#include <sys/mman.h> | |
#include <assert.h> | |
struct Foo { | |
int a; | |
void *ptr; | |
Foo() : a(4) { | |
ptr = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); | |
assert(ptr != MAP_FAILED); | |
} | |
~Foo() { | |
munmap(ptr, 4096); | |
} | |
}; | |
thread_local Foo foo; | |
static void f1(int a) { | |
foo.a += a; | |
} | |
int main() { | |
for (int i = 0; i < 10000; i++) { | |
std::thread t1(f1, 1); | |
std::thread t2(f1, 2); | |
t1.join(); | |
t2.join(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment