#include <teiacare/sdk/blocking_queue.hpp>
#include <spdlog/fmt/fmt.h>
#include <spdlog/spdlog.h>
#include <sstream>
using namespace std::chrono_literals;
template <typename T>
struct fmt::formatter<tc::sdk::blocking_queue<T>>
{
constexpr auto parse(format_parse_context& ctx)
{
return ctx.begin();
}
{
std::stringstream os;
{
os << ", ";
}
return fmt::format_to(ctx.out(), "[{}]", os.str());
}
};
int main()
{
spdlog::set_pattern("[%H:%M:%S.%e] %v");
spdlog::set_level(spdlog::level::trace);
{
spdlog::info("capacity: {}, size: {}", q.capacity(), q.size());
spdlog::info("queue: {}", q);
q.push(1);
q.push(2);
q.push(3);
q.pop();
q.push(4);
spdlog::info("capacity: {}, size: {}", q.capacity(), q.size());
spdlog::info("queue: {}", q);
}
{
q.push("a");
q.push("b");
q.push("c");
q.pop();
q.push("d");
spdlog::info("capacity: {}, size: {}", q.capacity(), q.size());
spdlog::info("queue: {}", q);
}
{
auto consumer_thread = std::thread([&] {
for (int i = 0; i < 10; ++i)
{
auto item = q.pop();
spdlog::debug("consumed: {}", item);
}
spdlog::warn("consumer finished!");
});
auto producer_thread = std::thread([&] {
for (int i = 0; i < 10; ++i)
{
q.push(i + 1);
spdlog::debug("produced: {}", i);
}
spdlog::warn("producer finished!");
});
producer_thread.join();
consumer_thread.join();
spdlog::info("finished!");
}
return 0;
}
Thread safe, blocking queue.
T pop()
Retrieve an item from the queue.
size_t size() const
Get the number of items currently in the queue.