Skip to content

hcbcomeup/cppbase

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cppbase

A lightweight, dependency-free, cross-platform C++17 basic library with thread pool, memory pool, message queue, timer manager and common utilities.

Features

  • ThreadPool - High-performance thread pool with task queue and future support
  • ObjectPool - Memory pool for object reuse, reducing allocation overhead
  • CircularQueue - Lock-free single-producer single-consumer circular queue
  • MpmcBlockingQueue - Multi-producer multi-consumer blocking queue
  • TimerMgr - Timer manager with periodic and one-shot timer support
  • Utility - Common utilities for thread naming, etc.

Requirements

  • C++17 or higher
  • CMake 3.15+
  • POSIX-compatible system (Linux, macOS) or Windows

Quick Start

Include in your project

#include "cppbase/thread_pool.hpp"
#include "cppbase/timer_mgr.hpp"
#include "cppbase/object_pool.hpp"
#include "cppbase/circular_queue.hpp"
#include "cppbase/mpmc_blocking_queue.hpp"

ThreadPool Example

#include "cppbase/thread_pool.hpp"

cppbase::ThreadPool pool;
pool.Start(4, 100);

pool.AddTask([]()
{
    std::cout << "Task executed!" << std::endl;
});

int ret = pool.AddTask([]() {}, std::chrono::milliseconds(100));
if (ret == cppbase::ErrorCode::EC_TIMEOUT) {
    std::cout << "Queue full, timeout!" << std::endl;
}

auto future = pool.Submit([]()
{
    return 42;
});
int result = future.get();  // result = 42

TimerMgr Example

#include "cppbase/timer_mgr.hpp"

auto& mgr = cppbase::TimerMgr::Instance();

// One-shot timer (1 second)
auto timer = std::make_shared<cppbase::Timer>(
    1000000000,  // 1 second in nanoseconds
    []()
    {
        std::cout << "Timer fired!" << std::endl;
    }
);
mgr.AddTimer(timer);

// Periodic timer (every 500ms)
auto periodicTimer = std::make_shared<cppbase::Timer>(
    500000000,
    []()
    {
        std::cout << "Periodic timer!" << std::endl;
    },
    500000000  // cycle time
);
mgr.AddTimer(periodicTimer);

// Delete timer
mgr.DelTimer(timer);

ObjectPool Example

#include "cppbase/object_pool.hpp"

struct MyObject
{
    int id;
    // ...
};

cppbase::ObjectPool<MyObject> pool;
{
    auto obj = pool.Acquire();
    obj->id = 123;
}

// Reset pool (replaces with a fresh pool; outstanding objects remain valid)
pool.Reset();

// Pool still usable after reset
auto obj2 = pool.Acquire();
obj2->id = 456;

CircularQueue Example

#include "cppbase/circular_queue.hpp"

cppbase::CircularQueue<int> queue(1024);

queue.PushBack(1);
queue.PushBack(2);

int value = queue.Front();
queue.PopFront();  // value = 1

MpmcBlockingQueue Example

#include "cppbase/mpmc_blocking_queue.hpp"

cppbase::MpmcBlockingQueue<int> queue(1024);

// Producer thread
queue.Enqueue(1);

// Consumer thread
int value;
queue.Dequeue(value);  // blocking until data available

Build

mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make

Run Examples

./examples/threadpool_example
./examples/timermgr_example
./examples/objectpool_example
./examples/circular_queue_example
./examples/mpmc_queue_example

Run Tests

ctest --output-on-failure
# Or run individual tests
./test/thread_pool_test
./test/timer_mgr_test
./test/object_pool_test
./test/circular_queue_test
./test/mpmc_queue_test

Components

Component Header Description
ThreadPool thread_pool.hpp Thread pool with task queue and future
TimerMgr timer_mgr.hpp Timer manager with one-shot and periodic timers
ObjectPool object_pool.hpp Memory pool for object reuse
CircularQueue circular_queue.hpp Lock-free SPSC circular queue
MpmcBlockingQueue mpmc_blocking_queue.hpp Multi-producer multi-consumer queue
NoCopyable nocopyable.hpp Base class for non-copyable objects
NoMoveable nomoveable.hpp Base class for non-movable objects
Utility utility.hpp Common utilities

License

MIT License

About

A lightweight, dependency-free, cross-platform C++17 basic library with thread pool, memory pool, message queue and common utilities.

Topics

Resources

License

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors