implemented a simple asynchronous logging infrastructure
This commit is contained in:
parent
b551ab7f27
commit
1ec41f2171
42
mpsc.cpp
42
mpsc.cpp
@ -3,68 +3,36 @@
|
||||
#include <thread>
|
||||
#include <atomic>
|
||||
|
||||
#include "data_structures/queue/mpsc_queue.hpp"
|
||||
#include "debug/log.hpp"
|
||||
|
||||
static constexpr int N = 1000;
|
||||
|
||||
std::atomic<bool> alive { true };
|
||||
|
||||
using q_t = lockfree::MpscQueue<std::string>;
|
||||
|
||||
void produce(q_t& q)
|
||||
void produce()
|
||||
{
|
||||
std::hash<std::thread::id> hasher;
|
||||
|
||||
for(int i = 0; i < N; ++i)
|
||||
{
|
||||
auto str = std::to_string(i) + " " +
|
||||
auto str = "Hello number " + std::to_string(i) + " from thread " +
|
||||
std::to_string(hasher(std::this_thread::get_id()));
|
||||
|
||||
q.push(std::make_unique<std::string>(str));
|
||||
}
|
||||
}
|
||||
|
||||
void consume(q_t& q)
|
||||
{
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
int i = 0;
|
||||
|
||||
while(true)
|
||||
{
|
||||
auto message = q.pop();
|
||||
|
||||
if(message != nullptr)
|
||||
{
|
||||
std::cerr << (i++) << ": " << *message << std::endl;
|
||||
continue;
|
||||
}
|
||||
|
||||
if(!alive)
|
||||
return;
|
||||
|
||||
std::this_thread::sleep_for(10ms);
|
||||
LOG_DEBUG(str);
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
constexpr int THREADS = 256;
|
||||
q_t q;
|
||||
|
||||
auto consumer = std::thread([&q]() { consume(q); });
|
||||
|
||||
std::vector<std::thread> threads;
|
||||
|
||||
for(int i = 0; i < THREADS; ++i)
|
||||
threads.push_back(std::thread([&q]() { produce(q); }));
|
||||
threads.push_back(std::thread([]() { produce(); }));
|
||||
|
||||
for(auto& thread : threads)
|
||||
thread.join();
|
||||
|
||||
std::cout << "FINISHED" << std::endl;
|
||||
alive.store(false);
|
||||
consumer.join();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
13
utils/bash_colors.hpp
Normal file
13
utils/bash_colors.hpp
Normal file
@ -0,0 +1,13 @@
|
||||
#ifndef MEMGRAPH_UTILS_BASH_COLORS_HPP
|
||||
#define MEMGRAPH_UTILS_BASH_COLORS_HPP
|
||||
|
||||
namespace bash_color
|
||||
{
|
||||
auto blue = "\033[94m";
|
||||
auto green = "\033[92m";
|
||||
auto yellow = "\033[93m";
|
||||
auto red = "\033[91m";
|
||||
auto end = "\033[0m";
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user