Skip to content

Instantly share code, notes, and snippets.

@wbenny
Created January 3, 2016 04:06
Show Gist options
  • Save wbenny/dffe3dc818f29ecf3973 to your computer and use it in GitHub Desktop.
Save wbenny/dffe3dc818f29ecf3973 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <thread>
#include <cstdlib>
#include <cstdint>
#include <cinttypes>
static uint64_t win = 0;
static uint64_t loss = 0;
void thread_printer()
{
for (;;)
{
printf("win/loss: %" PRIu64 "/%" PRIu64 " (%.3f%%)\n", win, loss, (float)win / loss*100.0f);
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
void thread_race(unsigned* address)
{
for (;;)
{
if (*address != *address)
{
win += 1;
}
else
{
loss += 1;
}
}
}
void thread_win(unsigned* address)
{
for (;;)
{
*address ^= 0x1;
}
}
int main()
{
unsigned variable = 123;
std::thread t0(&thread_printer);
std::thread t1(&thread_race, &variable);
std::thread t2(&thread_win, &variable);
t0.join();
t1.join();
t2.join();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment