add original source

This commit is contained in:
Bo Wang
2014-04-22 21:54:16 -04:00
parent f7a05be5e1
commit 6e45d14431
128 changed files with 5726 additions and 0 deletions

24
source/listing_8.12.cpp Normal file
View File

@@ -0,0 +1,24 @@
class barrier
{
unsigned const count;
std::atomic<unsigned> spaces;
std::atomic<unsigned> generation;
public:
explicit barrier(unsigned count_):
count(count_),spaces(count),generation(0)
{}
void wait()
{
unsigned const my_generation=generation;
if(!--spaces)
{
spaces=count;
++generation;
}
else
{
while(generation==my_generation)
std::this_thread::yield();
}
}
};