Demonstrates comprehensive crash protection mechanisms including signal handling, graceful shutdown, resource cleanup, scoped crash callbacks, and automatic recovery capabilities via crash_handler.
#include <iostream>
#include <thread>
#include <chrono>
#include <random>
#include <vector>
#include <memory>
#include <atomic>
public:
std::cout <<
"[ALLOC] Allocated critical resource: " <<
name_ << std::endl;
}
}
std::cout <<
"[CLEANUP] Cleaning up critical resource: " <<
name_ << std::endl;
}
}
const std::string&
name()
const {
return name_; }
private:
};
std::cout << "[CRASH] Simulating segmentation fault..." << std::endl;
int* null_ptr = nullptr;
*null_ptr = 42;
}
std::cout << "[CRASH] Simulating division by zero..." << std::endl;
volatile int zero = 0;
volatile int result = 100 / zero;
}
std::cout << "[CRASH] Simulating abort..." << std::endl;
std::abort();
}
std::cout << "[TASK] Task " << task_id << " starting normally" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(100 + (task_id % 200)));
std::cout << "[TASK] Task " << task_id << " completed successfully" << std::endl;
}
std::cout << "[WARN] Task " << task_id << " starting (potentially dangerous)" << std::endl;
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> crash_dist(1, 10);
int outcome = crash_dist(gen);
if (outcome <= 7) {
std::this_thread::sleep_for(std::chrono::milliseconds(50));
std::cout << "[TASK] Task " << task_id << " completed safely" << std::endl;
} else if (outcome == 8) {
} else if (outcome == 9) {
} else {
}
}
std::cout << "\n[ALERT] CRASH DETECTED!" << std::endl;
std::cout << "Time: " << std::chrono::duration_cast<std::chrono::seconds>(
context.
crash_time.time_since_epoch()).count() << std::endl;
std::cout << "Stack trace available" << std::endl;
}
}
std::cout << "[CLEANUP] Cleaning up global resources..." << std::endl;
if (resource && resource->is_allocated()) {
resource->cleanup();
}
}
}
std::cout << "[SAVE] Saving emergency state..." << std::endl;
std::cout <<
"Tasks failed: " <<
tasks_failed.load() << std::endl;
}
std::cout << "=== Thread System Crash Protection Demo ===" << std::endl;
std::cout << "This demo shows comprehensive crash protection mechanisms\n" << std::endl;
std::cout << "--- Step 1: Initialize Crash Protection ---" << std::endl;
std::cout << "[OK] Crash protection initialized" << std::endl;
std::cout << "\n--- Step 2: Allocate Critical Resources ---" << std::endl;
global_resources.push_back(std::make_shared<critical_resource>(
"DatabaseConnection"));
global_resources.push_back(std::make_shared<critical_resource>(
"NetworkSocket"));
global_resources.push_back(std::make_shared<critical_resource>(
"SharedMemory"));
std::cout << "\n--- Step 3: Create Thread Pool ---" << std::endl;
auto thread_pool = std::make_shared<thread_pool_t>(
"MainPool");
thread_pool_crash_safety::enable_for_pool(
"MainPool", *
thread_pool);
thread_pool_crash_safety::set_job_crash_handler(
std::cout << "[CRASH] Job crashed in pool: " << pool_name << std::endl;
std::cout <<
"Signal: " << context.
signal_name << std::endl;
});
{
std::vector<std::unique_ptr<thread_worker_t>> workers;
for (int i = 0; i < 4; ++i) {
workers.push_back(std::make_unique<thread_worker_t>());
}
if (r.is_err()) {
std::cerr << "Failed to add workers: " << r.error().message << std::endl;
return 1;
}
}
{
if (r.is_err()) {
std::cerr << "Failed to start thread pool: " << r.error().message << std::endl;
return 1;
}
}
std::cout << "[OK] Thread pool started with crash protection" << std::endl;
std::cout << "\n--- Step 4: Submit Normal Tasks ---" << std::endl;
for (int i = 0; i < 10; ++i) {
auto job = std::make_unique<callback_job>(
[i]() -> kcenon::common::VoidResult {
return kcenon::common::ok();
}
);
if (r.is_err()) {
std::cerr << "enqueue normal task failed: " << r.error().message << std::endl;
}
}
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << "[OK] All normal tasks completed" << std::endl;
std::cout << "\n--- Step 5: Submit Potentially Crashing Tasks ---" << std::endl;
std::cout << "[WARN] Some of these tasks may crash - crash protection will handle them" << std::endl;
for (int i = 10; i < 25; ++i) {
auto job = std::make_unique<callback_job>(
std::random_device rd; std::mt19937 gen(rd());
std::uniform_int_distribution<> dist(1, 10);
int outcome = dist(gen);
if (outcome >= 9) {
ctx.
crash_time = std::chrono::system_clock::now();
} else {
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
return kcenon::common::ok();
}
);
if (r.is_err()) {
std::cerr << "enqueue risky task failed: " << r.error().message << std::endl;
}
}
std::this_thread::sleep_for(std::chrono::seconds(2));
std::cout << "\n--- Step 6: Manual Crash Tests ---" << std::endl;
std::cout << "\nTest 1: Manual crash trigger" << std::endl;
test_context.
crash_time = std::chrono::system_clock::now();
std::cout << "\nTest 2: Scoped crash protection" << std::endl;
{
std::cout << "[PROTECT] Scoped crash handler activated" << std::endl;
}, 50);
std::cout << "[OK] Scoped protection active" << std::endl;
}
std::cout << "[OK] Scoped protection removed" << std::endl;
std::cout << "\n--- Step 7: Final Statistics ---" << std::endl;
std::cout << "Crash Statistics:" << std::endl;
std::cout << " Successful cleanups: " << stats.successful_cleanups << std::endl;
std::cout << " Failed cleanups: " << stats.failed_cleanups << std::endl;
std::cout << "\nTask Statistics:" << std::endl;
std::cout <<
" Tasks failed: " <<
tasks_failed.load() << std::endl;
std::cout << "\nResource Status:" << std::endl;
std::cout << " " << resource->name() << ": "
<< (resource->is_allocated() ? "allocated" : "cleaned up") << std::endl;
}
std::cout << "\n--- Step 8: Graceful Shutdown ---" << std::endl;
std::cout << "Stopping thread pool..." << std::endl;
{
if (r.is_err()) {
std::cerr << "Failed to stop thread pool: " << r.error().message << std::endl;
}
}
std::cout << "Cleaning up resources..." << std::endl;
std::cout << "Unregistering crash callbacks..." << std::endl;
std::cout << "\n=== Demo Completed Successfully ===" << std::endl;
std::cout << "Key features demonstrated:" << std::endl;
std::cout << "[OK] Signal handling and crash detection" << std::endl;
std::cout << "[OK] Stack trace generation" << std::endl;
std::cout << "[OK] Resource cleanup on crash" << std::endl;
std::cout << "[OK] Thread pool crash protection" << std::endl;
std::cout << "[OK] Scoped crash protection" << std::endl;
std::cout << "[OK] Graceful shutdown coordination" << std::endl;
std::cout << "[OK] Crash statistics and monitoring" << std::endl;
return 0;
}
Specialized job class that encapsulates user-defined callbacks.
bool is_allocated() const
const std::string & name() const
critical_resource(const std::string &name)
A specialized job class that encapsulates user-defined callbacks.
Thread-safe crash handler for the entire thread system.
void unregister_crash_callback(size_t registration_id)
Unregister a crash callback.
void initialize(crash_safety_level level=crash_safety_level::standard, bool enable_core_dumps=false)
Initialize crash handling with specified safety level.
void register_cleanup(const std::string &name, std::function< void()> cleanup, uint32_t timeout_ms=1000)
Register a resource cleanup function.
crash_stats get_stats() const
size_t register_crash_callback(const std::string &name, crash_callback callback, int priority=100)
Register a callback to be called during crash handling.
void set_crash_log_directory(const std::string &directory)
Set custom crash log directory.
void trigger_crash_handling(const crash_context &context)
Manually trigger crash handling (for testing)
Represents a unit of work (task) to be executed, typically by a job queue.
A template class representing either a value or an error.
RAII helper for automatic crash callback registration.
A thread pool for concurrent execution of jobs using multiple worker threads.
auto enqueue(std::unique_ptr< job > &&job) -> common::VoidResult
Enqueues a new job into the shared job_queue.
auto enqueue_batch(std::vector< std::unique_ptr< job > > &&jobs) -> common::VoidResult
Enqueues a batch of jobs into the shared job_queue.
auto stop(const bool &immediately_stop=false) -> common::VoidResult
Stops the thread pool and all worker threads.
auto start(void) -> common::VoidResult
Starts the thread pool and all associated workers.
A specialized worker thread that processes jobs from a job_queue.
Core thread pool implementation with work stealing and auto-scaling.
Crash safety levels and handler interface for thread failure recovery.
void potentially_crashing_task(int task_id)
void normal_task(int task_id)
std::atomic< int > tasks_completed
void simulate_division_by_zero()
std::vector< std::shared_ptr< critical_resource > > global_resources
void on_system_crash(const crash_context &context)
void emergency_state_save()
void cleanup_global_resources()
std::atomic< int > tasks_failed
void simulate_segmentation_fault()
std::atomic< bool > system_running
Core threading foundation of the thread system library.
Crash context information.
std::chrono::system_clock::time_point crash_time
std::thread::id crashing_thread
size_t total_crashes_handled
Specialized worker thread that processes jobs from a job_queue.