Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_threads.c
Go to the documentation of this file.
1
9#include <stdio.h>
10#include <errno.h>
11#include <string.h>
12
13#include "nilorea/n_log.h"
14#include "nilorea/n_time.h"
16
17void usage(void) {
18 fprintf(stderr,
19 " -v version\n"
20 " -h help\n"
21 " -V LOG_LEVEL (LOG_DEBUG,INFO,NOTICE,ERR)\n");
22}
23
24void process_args(int argc, char** argv) {
25 int getoptret = 0,
26 log_level = LOG_ERR; /* default log level */
27
28 while ((getoptret = getopt(argc, argv, "vhV:")) != EOF) {
29 switch (getoptret) {
30 case 'v':
31 fprintf(stderr, "Date de compilation : %s a %s.\n", __DATE__, __TIME__);
32 exit(1);
33 case 'V':
34 if (!strncmp("LOG_NULL", optarg, 5)) {
36 } else {
37 if (!strncmp("LOG_NOTICE", optarg, 6)) {
39 } else {
40 if (!strncmp("LOG_INFO", optarg, 7)) {
42 } else {
43 if (!strncmp("LOG_ERR", optarg, 5)) {
45 } else {
46 if (!strncmp("LOG_DEBUG", optarg, 5)) {
48 } else {
49 fprintf(stderr, "%s n'est pas un niveau de log valide.\n", optarg);
50 exit(-1);
51 }
52 }
53 }
54 }
55 }
56 break;
57 default:
58 case '?': {
59 if (optopt == 'V') {
60 fprintf(stderr, "\n Missing log level\n");
61 }
62 usage();
63 exit(1);
64 }
65 case 'h': {
66 usage();
67 exit(1);
68 }
69 } /* switch */
71 }
72} /* void process_args( ... ) */
73
74void* occupy_thread(void* rest) {
75 __n_assert(rest, return NULL);
76
77 intptr_t sleep_value = (intptr_t)(rest);
78
79 n_log(LOG_DEBUG, "Starting to sleep %d usecs on thread %lld", sleep_value, pthread_self());
80
81 if (sleep_value < 1000000) {
82 usleep(sleep_value);
83 } else {
84 usleep((sleep_value) % 1000000);
85 sleep((sleep_value / 1000000));
86 }
87
88 n_log(LOG_DEBUG, "End of sleep %d usecs on thread %lld", sleep_value, pthread_self());
89
90 return NULL;
91}
92
93int main(int argc, char** argv) {
94 int nb_active_threads = get_nb_cpu_cores();
95 int nb_waiting_threads = 2 * nb_active_threads;
96 int nb_total_threads = (nb_active_threads + nb_waiting_threads);
97
99
100 // processing args and set log_level
101 process_args(argc, argv);
102
103 n_log(LOG_INFO, "Creating a new thread pool of %d active and %d waiting threads", nb_active_threads, nb_waiting_threads);
104 THREAD_POOL* thread_pool = new_thread_pool(nb_active_threads, nb_waiting_threads);
105
106 n_log(LOG_INFO, "Adding new %d new tasks...", nb_total_threads);
107 for (int it = 0; it < nb_total_threads; it++) {
108 n_log(LOG_INFO, "adding task %d", it);
109 // sleep time as a payload to the occupy_thread
110 int sleep_value = 1 + rand() % 1000000;
111 // add task and payload
112 if (add_threaded_process(thread_pool, &occupy_thread, (void*)(intptr_t)sleep_value, NORMAL_PROC) == FALSE) {
113 n_log(LOG_ERR, "Error adding client management to thread pool");
114 }
115 }
116 n_log(LOG_INFO, "Adding tasks done. Waiting for pool thread to complete the tasks...");
118
119 n_log(LOG_INFO, "Task completed. Destroying pool...");
121
122 n_log(LOG_INFO, "Destroyed.");
123
124 exit(0);
125} /* END_OF_MAIN() */
void usage(void)
Definition ex_common.c:22
void process_args(int argc, char **argv)
Definition ex_common.c:29
int main(void)
THREAD_POOL * thread_pool
Definition ex_fluid.c:59
int getoptret
Definition ex_fluid.c:42
int log_level
Definition ex_fluid.c:43
void * occupy_thread(void *rest)
Definition ex_threads.c:74
#define __n_assert(__ptr, __ret)
macro to assert things
Definition n_common.h:256
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:70
#define LOG_DEBUG
debug-level messages
Definition n_log.h:65
#define LOG_ERR
error conditions
Definition n_log.h:57
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:104
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:61
#define LOG_NULL
no log output
Definition n_log.h:27
#define LOG_INFO
informational
Definition n_log.h:63
#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)
delete a thread_pool, exit the threads and free the structs
long int get_nb_cpu_cores()
get number of core of current system
int wait_for_threaded_pool(THREAD_POOL *thread_pool, unsigned int delay)
Wait for all the launched process in the thread pool to terminate.
Structure of a trhead pool.
Generic log system.
Thread pool declaration.
Timing utilities.