Skip to content

Instantly share code, notes, and snippets.

@Lazin
Created June 1, 2012 08:31
Show Gist options
  • Save Lazin/2850346 to your computer and use it in GitHub Desktop.
Save Lazin/2850346 to your computer and use it in GitHub Desktop.
class ConcurrentCounter {
unsigned int* counters_;
int length_;
public:
ConcurrentCounter() {
length_ = boost::thread::hardware_concurrency() * 0x100;
counters_ = new unsigned int[length_];
std::fill(counters_, counters_ + length_, 0);
}
unsigned int get_value() {
return std::accumulate(counters_, counters_ + length_, 0);
}
void inc() {
auto i = rand() % length_;
InterlockedIncrement(counters_ + i);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment