Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_thread_pool_discard.c File Reference

destroy_threaded_pool() does not drop queued jobs silently. More...

#include "nilorea/n_common.h"
#include "nilorea/n_log.h"
#include "nilorea/n_thread_pool.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
Include dependency graph for ex_thread_pool_discard.c:

Go to the source code of this file.

Functions

static void discard_job (void *p)
 Discard function: releases a param that will never be run.
int main (void)
static int run (unsigned int delay)
 Queue four slow jobs on a one thread pool and destroy it.
static void * slow_job (void *p)
 A job that takes 300 ms and releases its own param.

Variables

static pthread_mutex_t count_lock = PTHREAD_MUTEX_INITIALIZER
 Guards the two counters.
static int discarded = 0
static int ran = 0
 Jobs that ran, and params the discard function released.

Detailed Description

destroy_threaded_pool() does not drop queued jobs silently.

One thread, one slow job running and three queued behind it. With a retry budget shorter than the running job, destroy gives up waiting, joins the thread - which still waits for the running job - and discards the three queued ones without running them. Their params used to go with them, unreleased and unmentioned; under a leak checker that was three leaks and nowhere else was it visible.

Now each discarded param is handed to the pool's discard function. This checks that exactly the three queued jobs are discarded, that the running one completes, and that a budget long enough to drain the queue runs all four and discards none. Run under ASan, a param that nobody releases fails the run.

Author
Castagnier Mickael
Version
1.0
Date
22/09/2026

Definition in file ex_thread_pool_discard.c.

Function Documentation

◆ discard_job()

void discard_job ( void * p)
static

Discard function: releases a param that will never be run.

Parameters
pA malloc'd int.

Definition at line 65 of file ex_thread_pool_discard.c.

References count_lock, and discarded.

Referenced by run().

Here is the caller graph for this function:

◆ main()

int main ( void )

Definition at line 98 of file ex_thread_pool_discard.c.

References discarded, failures, LOG_NULL, ran, run, and set_log_level().

Here is the call graph for this function:

◆ run()

int run ( unsigned int delay)
static

Queue four slow jobs on a one thread pool and destroy it.

Parameters
delaydestroy_threaded_pool()'s retry delay, in microseconds.
Returns
0 when every job was set up, 1 otherwise.

Definition at line 77 of file ex_thread_pool_discard.c.

References add_threaded_process(), destroy_threaded_pool(), discard_job(), discarded, new_thread_pool(), NORMAL_PROC, pool, ran, set_thread_pool_discard(), and slow_job().

Here is the call graph for this function:

◆ slow_job()

void * slow_job ( void * p)
static

A job that takes 300 ms and releases its own param.

Parameters
pA malloc'd int.
Returns
NULL

Definition at line 52 of file ex_thread_pool_discard.c.

References count_lock, and ran.

Referenced by run().

Here is the caller graph for this function:

Variable Documentation

◆ count_lock

pthread_mutex_t count_lock = PTHREAD_MUTEX_INITIALIZER
static

Guards the two counters.

Definition at line 45 of file ex_thread_pool_discard.c.

Referenced by discard_job(), and slow_job().

◆ discarded

int discarded = 0
static

Definition at line 43 of file ex_thread_pool_discard.c.

Referenced by discard_job(), main(), and run().

◆ ran

int ran = 0
static

Jobs that ran, and params the discard function released.

Definition at line 43 of file ex_thread_pool_discard.c.

Referenced by main(), run(), and slow_job().