Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_thread_pool_discard.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * SPDX-License-Identifier: Apache-2.0
12 */
13
34#include "nilorea/n_common.h"
35#include "nilorea/n_log.h"
37
38#include <stdio.h>
39#include <stdlib.h>
40#include <unistd.h>
41
43static int ran = 0, discarded = 0;
45static pthread_mutex_t count_lock = PTHREAD_MUTEX_INITIALIZER;
46
52static void* slow_job(void* p) {
53 usleep(300000);
54 pthread_mutex_lock(&count_lock);
55 ran++;
56 pthread_mutex_unlock(&count_lock);
57 free(p);
58 return NULL;
59}
60
65static void discard_job(void* p) {
66 pthread_mutex_lock(&count_lock);
67 discarded++;
68 pthread_mutex_unlock(&count_lock);
69 free(p);
70}
71
77static int run(unsigned int delay) {
79 int i = 0;
80
81 if (!pool) return 1;
83 ran = discarded = 0;
84 for (i = 0; i < 4; i++) {
85 int* p = malloc(sizeof(int));
86 if (!p) return 1;
87 *p = i;
89 free(p);
90 return 1;
91 }
92 }
93 usleep(20000); /* let the first one start */
95 return 0;
96}
97
98int main(void) {
99 int failures = 0;
100
101 /* The error line destroy logs for the short budget is the expected
102 outcome here, so it is not shown. */
104
105 /* 1000 retries of 100 us is 0.1 s, shorter than the running job. */
106 if (run(100) != 0) failures++;
107 if (ran != 1 || discarded != 3) {
108 fprintf(stderr, "short budget: %d ran and %d discarded, wanted 1 and 3\n", ran, discarded);
109 failures++;
110 }
111 /* 1000 retries of 1 ms is a second: the queue drains inside it. */
112 if (run(1000) != 0) failures++;
113 if (ran != 4 || discarded != 0) {
114 fprintf(stderr, "long budget: %d ran and %d discarded, wanted 4 and 0\n", ran, discarded);
115 failures++;
116 }
117 if (failures) {
118 fprintf(stderr, "ex_thread_pool_discard: %d failure(s)\n", failures);
119 return 1;
120 }
121 printf("ex_thread_pool_discard: all checks passed\n");
122 return 0;
123}
static int failures
Definition ex_atlas.c:51
static NETWORK_POOL * pool
int run
Definition ex_kafka.c:56
static int discarded
static pthread_mutex_t count_lock
Guards the two counters.
int main(void)
static int ran
Jobs that ran, and params the discard function released.
static void * slow_job(void *p)
A job that takes 300 ms and releases its own param.
static void discard_job(void *p)
Discard function: releases a param that will never be run.
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL ).
Definition n_log.c:123
#define LOG_NULL
no log output
Definition n_log.h:70
#define NORMAL_PROC
processing mode for added func, synced start, can be queued
THREAD_POOL * new_thread_pool(size_t nbmaxthr, size_t nb_max_waiting)
Create a new pool of nbmaxthr threads.
int add_threaded_process(THREAD_POOL *thread_pool, void *(*func_ptr)(void *param), void *param, int mode)
add a function and params to a thread pool
int destroy_threaded_pool(THREAD_POOL **pool, unsigned int delay)
Stop every thread of a pool and free it.
int set_thread_pool_discard(THREAD_POOL *thread_pool, void(*discard)(void *param))
Set what destroy_threaded_pool() does with the param of a queued job it discards.
Structure of a thread pool.
Common headers and low-level functions & define.
Generic log system.
Thread pool declaration.