Commit f35af2fa authored by Scott Vokes's avatar Scott Vokes
Browse files

Add a thread that loops forever to test thread cancellation.

parent 18cc1b53
Loading
Loading
Loading
Loading
+27 −3
Original line number Diff line number Diff line
@@ -65,6 +65,13 @@ static void task_cb(void *udata) {
    dump_stats("", &stats);
}

static void inf_loop_cb(void *env) {
    (void)env;
    for (;;) {
        sleep(1);
    }
}

int main(int argc, char **argv) {
    uint8_t sz2 = 8;
    uint8_t max_threads = 8;
@@ -112,11 +119,28 @@ int main(int argc, char **argv) {
        sleep(1);
    }

    printf("shutting down...\n");
    while (!threadpool_shutdown(t, false)) {
        printf("shutting down...\n");
    task.task = inf_loop_cb;
    size_t counterpressure = 0;
    while (!threadpool_schedule(t, &task, &counterpressure)) {
        usleep(10 * 1000);
    }

    sleep(1);

    const int THREAD_SHUTDOWN_SECONDS = 5;
    printf("shutting down...\n");

    int limit = (1000 * THREAD_SHUTDOWN_SECONDS)/10;
    for (int i = 0; i < limit; i++) {
        if (threadpool_shutdown(t, false)) { break; }
        (void)poll(NULL, 0, 10);

        if (i == limit - 1) {
            printf("cancelling thread in intentional infinite loop\n");
            threadpool_shutdown(t, true);
        }
    }
    threadpool_free(t);

    return 0;
}