Skip to content

Instantly share code, notes, and snippets.

@snoopspy
Created April 11, 2015 16:26
Show Gist options
  • Save snoopspy/b65036fb52b7778ce19c to your computer and use it in GitHub Desktop.
Save snoopspy/b65036fb52b7778ce19c to your computer and use it in GitHub Desktop.
#include <algorithm>
#include <thread>
#include <glog/logging.h>
using namespace std;
volatile uint64_t g_value;
void write_proc() {
constexpr int CNT = 1000000;
for (uint32_t i = 0; i < CNT; i++) {
uint32_t upper = i;
uint32_t lower = upper;
uint64_t v = ((uint64_t)upper << 32) | lower;
g_value = v;
}
}
void read_proc() {
constexpr int CNT = 1000000;
for (int i = 0; i < CNT; i++) {
uint64_t v = g_value;
uint32_t upper = v >> 32;
uint32_t lower = v;
if (upper != lower) {
LOG(ERROR) << "oops!!!" << hex << upper << ' ' << hex << lower;
}
}
}
int main() {
constexpr int THREAD_CNT = 5;
thread write_thread;
thread read_thread[THREAD_CNT];
write_thread = thread(write_proc);
for (int i = 0; i < THREAD_CNT; i++) read_thread[i] = thread(read_proc);
write_thread.join();
for (int i = 0; i < THREAD_CNT; i++) read_thread[i].join();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment