Created
June 1, 2012 08:31
-
-
Save Lazin/2850346 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
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