add original source
This commit is contained in:
24
source/listing_8.12.cpp
Normal file
24
source/listing_8.12.cpp
Normal 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();
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user