add original source
This commit is contained in:
41
source/listing_5.11.cpp
Normal file
41
source/listing_5.11.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
|
||||
std::vector<int> queue_data;
|
||||
std::atomic<int> count;
|
||||
|
||||
void populate_queue()
|
||||
{
|
||||
unsigned const number_of_items=20;
|
||||
queue_data.clear();
|
||||
for(unsigned i=0;i<number_of_items;++i)
|
||||
{
|
||||
queue_data.push_back(i);
|
||||
}
|
||||
|
||||
count.store(number_of_items,std::memory_order_release);
|
||||
}
|
||||
|
||||
void consume_queue_items()
|
||||
{
|
||||
while(true)
|
||||
{
|
||||
int item_index;
|
||||
if((item_index=count.fetch_sub(1,std::memory_order_acquire))<=0)
|
||||
{
|
||||
wait_for_more_items();
|
||||
continue;
|
||||
}
|
||||
process(queue_data[item_index-1]);
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
std::thread a(populate_queue);
|
||||
std::thread b(consume_queue_items);
|
||||
std::thread c(consume_queue_items);
|
||||
a.join();
|
||||
b.join();
|
||||
c.join();
|
||||
}
|
||||
Reference in New Issue
Block a user