#include <iostream>
#include <string>
#include <thread>
int main(int argc, char** argv)
{
std::cout << "Please enter some text. Enter '#' to exit..." << std::endl;
auto consumer = std::thread([&buffer]()
{
const size_t local_capacity = 1024;
char local_buffer[local_capacity];
size_t local_size;
do
{
while (!buffer.
Dequeue(local_buffer, local_size = local_capacity))
std::this_thread::yield();
std::cout << "Your entered: ";
std::cout.write(local_buffer, local_size);
std::cout << std::endl;
} while (memchr(local_buffer, '#', local_size) == nullptr);
});
std::string line;
while (getline(std::cin, line))
{
while (!buffer.
Enqueue(line.c_str(), line.size()))
std::this_thread::yield();
if (line.find('#') != std::string::npos)
break;
}
consumer.join();
return 0;
}
Multiple producers / single consumer wait-free ring buffer.
bool Enqueue(const void *data, size_t size)
Enqueue a data into the ring buffer (single producer thread method)
bool Dequeue(void *data, size_t &size)
Dequeue a data from the ring buffer (single consumer thread method)
Multiple producers / single consumer wait-free ring buffer definition.