43 std::cout <<
"[ALLOC] Allocated critical resource: " <<
name_ << std::endl;
52 std::cout <<
"[CLEANUP] Cleaning up critical resource: " <<
name_ << std::endl;
70 std::cout <<
"[CRASH] Simulating segmentation fault..." << std::endl;
71 int* null_ptr =
nullptr;
76 std::cout <<
"[CRASH] Simulating division by zero..." << std::endl;
77 volatile int zero = 0;
78 volatile int result = 100 / zero;
83 std::cout <<
"[CRASH] Simulating abort..." << std::endl;
89 std::cout <<
"[TASK] Task " << task_id <<
" starting normally" << std::endl;
92 std::this_thread::sleep_for(std::chrono::milliseconds(100 + (task_id % 200)));
95 std::cout <<
"[TASK] Task " << task_id <<
" completed successfully" << std::endl;
99 std::cout <<
"[WARN] Task " << task_id <<
" starting (potentially dangerous)" << std::endl;
102 std::random_device rd;
103 std::mt19937 gen(rd());
104 std::uniform_int_distribution<> crash_dist(1, 10);
106 int outcome = crash_dist(gen);
110 std::this_thread::sleep_for(std::chrono::milliseconds(50));
112 std::cout <<
"[TASK] Task " << task_id <<
" completed safely" << std::endl;
113 }
else if (outcome == 8) {
117 }
else if (outcome == 9) {
130 std::cout <<
"\n[ALERT] CRASH DETECTED!" << std::endl;
133 std::cout <<
"Time: " << std::chrono::duration_cast<std::chrono::seconds>(
134 context.
crash_time.time_since_epoch()).count() << std::endl;
137 std::cout <<
"Stack trace available" << std::endl;
145 std::cout <<
"[CLEANUP] Cleaning up global resources..." << std::endl;
147 if (resource && resource->is_allocated()) {
155 std::cout <<
"[SAVE] Saving emergency state..." << std::endl;
156 std::cout <<
"Tasks completed: " <<
tasks_completed.load() << std::endl;
157 std::cout <<
"Tasks failed: " <<
tasks_failed.load() << std::endl;
158 std::cout <<
"Resources allocated: " <<
global_resources.size() << std::endl;
162 std::cout <<
"=== Thread System Crash Protection Demo ===" << std::endl;
163 std::cout <<
"This demo shows comprehensive crash protection mechanisms\n" << std::endl;
166 std::cout <<
"--- Step 1: Initialize Crash Protection ---" << std::endl;
180 std::cout <<
"[OK] Crash protection initialized" << std::endl;
183 std::cout <<
"\n--- Step 2: Allocate Critical Resources ---" << std::endl;
185 global_resources.push_back(std::make_shared<critical_resource>(
"DatabaseConnection"));
186 global_resources.push_back(std::make_shared<critical_resource>(
"NetworkSocket"));
187 global_resources.push_back(std::make_shared<critical_resource>(
"FileHandle"));
188 global_resources.push_back(std::make_shared<critical_resource>(
"SharedMemory"));
191 std::cout <<
"\n--- Step 3: Create Thread Pool ---" << std::endl;
197 auto thread_pool = std::make_shared<thread_pool_t>(
"MainPool");
202 [](
const std::string& pool_name,
const crash_context& context) {
203 std::cout <<
"[CRASH] Job crashed in pool: " << pool_name << std::endl;
204 std::cout <<
"Signal: " << context.
signal_name << std::endl;
209 std::vector<std::unique_ptr<thread_worker_t>> workers;
210 for (
int i = 0; i < 4; ++i) {
211 workers.push_back(std::make_unique<thread_worker_t>());
215 std::cerr <<
"Failed to add workers: " << r.error().message << std::endl;
223 std::cerr <<
"Failed to start thread pool: " << r.error().message << std::endl;
227 std::cout <<
"[OK] Thread pool started with crash protection" << std::endl;
230 std::cout <<
"\n--- Step 4: Submit Normal Tasks ---" << std::endl;
232 for (
int i = 0; i < 10; ++i) {
233 auto job = std::make_unique<callback_job>(
234 [i]() -> kcenon::common::VoidResult {
236 return kcenon::common::ok();
241 std::cerr <<
"enqueue normal task failed: " << r.error().message << std::endl;
245 std::this_thread::sleep_for(std::chrono::seconds(1));
247 std::cout <<
"[OK] All normal tasks completed" << std::endl;
250 std::cout <<
"\n--- Step 5: Submit Potentially Crashing Tasks ---" << std::endl;
251 std::cout <<
"[WARN] Some of these tasks may crash - crash protection will handle them" << std::endl;
253 for (
int i = 10; i < 25; ++i) {
254 auto job = std::make_unique<callback_job>(
257 std::random_device rd; std::mt19937 gen(rd());
258 std::uniform_int_distribution<> dist(1, 10);
259 int outcome = dist(gen);
266 ctx.
crash_time = std::chrono::system_clock::now();
271 std::this_thread::sleep_for(std::chrono::milliseconds(50));
274 return kcenon::common::ok();
279 std::cerr <<
"enqueue risky task failed: " << r.error().message << std::endl;
282 std::this_thread::sleep_for(std::chrono::seconds(2));
285 std::cout <<
"\n--- Step 6: Manual Crash Tests ---" << std::endl;
288 std::cout <<
"\nTest 1: Manual crash trigger" << std::endl;
293 test_context.
stack_trace =
"Manual test stack trace";
294 test_context.
crash_time = std::chrono::system_clock::now();
300 std::cout <<
"\nTest 2: Scoped crash protection" << std::endl;
304 std::cout <<
"[PROTECT] Scoped crash handler activated" << std::endl;
307 std::cout <<
"[OK] Scoped protection active" << std::endl;
310 std::cout <<
"[OK] Scoped protection removed" << std::endl;
313 std::cout <<
"\n--- Step 7: Final Statistics ---" << std::endl;
316 std::cout <<
"Crash Statistics:" << std::endl;
318 std::cout <<
" Successful cleanups: " << stats.successful_cleanups << std::endl;
319 std::cout <<
" Failed cleanups: " << stats.failed_cleanups << std::endl;
321 std::cout <<
"\nTask Statistics:" << std::endl;
322 std::cout <<
" Tasks completed: " <<
tasks_completed.load() << std::endl;
323 std::cout <<
" Tasks failed: " <<
tasks_failed.load() << std::endl;
325 std::cout <<
"\nResource Status:" << std::endl;
327 std::cout <<
" " << resource->name() <<
": "
328 << (resource->is_allocated() ?
"allocated" :
"cleaned up") << std::endl;
332 std::cout <<
"\n--- Step 8: Graceful Shutdown ---" << std::endl;
334 std::cout <<
"Stopping thread pool..." << std::endl;
338 std::cerr <<
"Failed to stop thread pool: " << r.error().message << std::endl;
342 std::cout <<
"Cleaning up resources..." << std::endl;
345 std::cout <<
"Unregistering crash callbacks..." << std::endl;
348 std::cout <<
"\n=== Demo Completed Successfully ===" << std::endl;
349 std::cout <<
"Key features demonstrated:" << std::endl;
350 std::cout <<
"[OK] Signal handling and crash detection" << std::endl;
351 std::cout <<
"[OK] Stack trace generation" << std::endl;
352 std::cout <<
"[OK] Resource cleanup on crash" << std::endl;
353 std::cout <<
"[OK] Thread pool crash protection" << std::endl;
354 std::cout <<
"[OK] Scoped crash protection" << std::endl;
355 std::cout <<
"[OK] Graceful shutdown coordination" << std::endl;
356 std::cout <<
"[OK] Crash statistics and monitoring" << std::endl;
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.
static crash_handler & instance()
Get the global crash handler instance.
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.
static void set_job_crash_handler(std::function< void(const std::string &pool_name, const crash_context &)> handler)
Register job crash handler.
static void enable_for_pool(const std::string &pool_name, class thread_pool &pool)
Enable crash safety for a thread pool.
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.