39 " -V LOG_LEVEL (LOG_DEBUG,INFO,NOTICE,ERR)\n");
46 while ((
getoptret = getopt(argc, argv,
"vhV:")) != EOF) {
49 fprintf(stderr,
"Date de compilation : %s a %s.\n", __DATE__, __TIME__);
52 if (!strcmp(
"LOG_NULL", optarg))
54 else if (!strcmp(
"LOG_NOTICE", optarg))
56 else if (!strcmp(
"LOG_INFO", optarg))
58 else if (!strcmp(
"LOG_ERR", optarg))
60 else if (!strcmp(
"LOG_DEBUG", optarg))
63 fprintf(stderr,
"%s n'est pas un niveau de log valide.\n", optarg);
70 fprintf(stderr,
"\n Missing log level\n");
87 intptr_t sleep_value = (intptr_t)(rest);
89 n_log(
LOG_DEBUG,
"Starting to sleep %d usecs on thread %lld", sleep_value, pthread_self());
91 if (sleep_value < 1000000) {
92 usleep((
unsigned int)sleep_value);
94 usleep((
unsigned int)((sleep_value) % 1000000));
95 sleep((
unsigned int)(sleep_value / 1000000));
98 n_log(
LOG_DEBUG,
"End of sleep %d usecs on thread %lld", sleep_value, pthread_self());
103int main(
int argc,
char** argv) {
105 int nb_active_threads = (cores > 0) ? (
int)cores : 1;
106 int nb_waiting_threads = 2 * nb_active_threads;
107 int nb_total_threads = (nb_active_threads + nb_waiting_threads);
114 n_log(
LOG_INFO,
"Creating a new thread pool of %d active and %d waiting threads", nb_active_threads, nb_waiting_threads);
117 n_log(
LOG_INFO,
"Adding new %d new tasks...", nb_total_threads);
118 for (
int it = 0; it < nb_total_threads; it++) {
121 int sleep_value = 1 + rand() % 1000000;
124 n_log(
LOG_ERR,
"Error adding client management to thread pool");
127 n_log(
LOG_INFO,
"Adding tasks done. Waiting for pool thread to complete the tasks...");
137 for (
int it = 0; it < nb_active_threads; it++) {
138 int sleep_value = 1 + rand() % 500000;
149 n_log(
LOG_INFO,
"Synced pool done. Testing refresh_thread_pool...");
void process_args(int argc, char **argv)
THREAD_POOL * thread_pool
void * occupy_thread(void *rest)
#define __n_assert(__ptr, __ret)
macro to assert things
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_DEBUG
debug-level messages
#define LOG_ERR
error conditions
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
#define LOG_NOTICE
normal but significant condition
#define LOG_NULL
no log output
#define LOG_INFO
informational
#define NORMAL_PROC
processing mode for added func, synced start, can be queued
int start_threaded_pool(THREAD_POOL *thread_pool)
Launch the process waiting for execution in the thread pool.
THREAD_POOL * new_thread_pool(size_t nbmaxthr, size_t nb_max_waiting)
Create a new pool of nbmaxthr threads.
#define SYNCED_PROC
processing mode for added func, synced start, not queued
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 refresh_thread_pool(THREAD_POOL *thread_pool)
try to add some waiting DIRECT_PROCs on some free thread slots, else do nothing
int wait_for_threaded_pool(THREAD_POOL *thread_pool)
Wait for the thread pool to become idle (no active threads, empty waiting list), blocking without pol...
int destroy_threaded_pool(THREAD_POOL **pool, unsigned int delay)
delete a thread_pool, exit the threads and free the structs
int wait_for_synced_threaded_pool(THREAD_POOL *thread_pool)
wait for all the launched process, blocking but light on the CPU as there is no polling
long int get_nb_cpu_cores()
get number of core of current system
Structure of a thread pool.