Skip to content

Error Handling Example

Proper error handling patterns.

C Version

c
#include <fastqueue/fastqueue.h>
#include <stdio.h>
#include <stdlib.h>

static void safe_task(void *arg)
{
    int id = *(int *)arg;
    printf("Task %d executed safely\n", id);
}

int main(void)
{
    fq_status_t st;

    // 1. Check creation
    fq_thread_pool_t *pool = NULL;
    st = fq_thread_pool_create_ex(&pool, 4);
    if (st != FQ_OK) {
        fprintf(stderr, "Pool creation failed: %s (code=%d)\n",
                fq_status_string(st), st);
        return 1;
    }

    // 2. Check submissions
    for (int i = 0; i < 10; ++i) {
        int *id = malloc(sizeof(int));
        if (!id) {
            fprintf(stderr, "Memory allocation failed\n");
            continue;
        }
        *id = i;

        st = fq_thread_pool_submit_fn(pool, safe_task, id);
        if (st != FQ_OK) {
            fprintf(stderr, "Submit task %d failed: %s\n",
                    i, fq_status_string(st));
            free(id);
        }
    }

    // 3. Wait and check
    fq_thread_pool_wait_idle(pool);

    // 4. Shutdown
    st = fq_thread_pool_shutdown(pool);
    if (st != FQ_OK) {
        fprintf(stderr, "Shutdown failed: %s\n", fq_status_string(st));
        return 1;
    }

    printf("All operations completed successfully.\n");
    return 0;
}

Scheduler with Error Checking

c
#include <fastqueue/fastqueue.h>
#include <stdio.h>

static void validated_task(void *arg)
{
    if (!arg) {
        fprintf(stderr, "Task received NULL argument\n");
        return;
    }
    printf("Result: %d\n", *(int *)arg);
}

int main(void)
{
    fq_scheduler_t *scheduler = NULL;
    fq_scheduler_config_t cfg;
    fq_scheduler_config_default(&cfg);
    cfg.thread_count = 2;

    fq_status_t st = fq_scheduler_create(&scheduler, &cfg);
    if (st != FQ_OK) {
        fprintf(stderr, "Scheduler create: %s\n", fq_status_string(st));
        return 1;
    }

    int values[] = {10, 20, 30, 40, 50};
    for (int i = 0; i < 5; ++i) {
        st = fq_scheduler_submit_fn(scheduler, validated_task, &values[i]);
        if (st != FQ_OK) {
            fprintf(stderr, "Submit %d: %s\n", i, fq_status_string(st));
        }
    }

    fq_scheduler_wait_idle(scheduler);

    fq_scheduler_stats_t stats;
    fq_scheduler_stats(scheduler, &stats);
    printf("Stats: submitted=%lu completed=%lu\n",
           stats.tasks_submitted, stats.tasks_completed);

    fq_scheduler_shutdown(scheduler);
    return 0;
}

C++ Version

cpp
#include <fastqueue/fastqueue.h>
#include <iostream>
#include <memory>

int main()
{
    fq_thread_pool_t *pool = nullptr;
    fq_status_t st = fq_thread_pool_create_ex(&pool, 4);

    if (st != FQ_OK) {
        std::cerr << "Failed: " << fq_status_string(st) << "\n";
        return 1;
    }

    for (int i = 0; i < 10; ++i) {
        auto id = std::make_unique<int>(i);
        st = fq_thread_pool_submit_fn(pool, [](void *arg) {
            std::cout << "Task " << *static_cast<int *>(arg) << "\n";
        }, id.get());

        if (st != FQ_OK) {
            std::cerr << "Submit failed: " << fq_status_string(st) << "\n";
        } else {
            id.release(); // Transfer ownership to pool
        }
    }

    fq_thread_pool_wait_idle(pool);
    fq_thread_pool_shutdown(pool);
    return 0;
}

Error Codes Reference

CodeNameMeaning
0FQ_OKSuccess
-1FQ_ERR_NOMEMOut of memory
-2FQ_ERR_INVALInvalid argument
-3FQ_ERR_BUSYResource busy
-4FQ_ERR_TIMEOUTOperation timed out
-5FQ_ERR_CANCELEDOperation was canceled
-6FQ_ERR_OVERFLOWQueue overflow
-7FQ_ERR_CLOSEDResource closed
-8FQ_ERR_INTERNALInternal error
-9FQ_ERR_NOSUPPORTNot supported

Released under the MIT License.