Shadow Network Framework  0.0.2-alpha
C Server Library for Shadow Network Framework
Loading...
Searching...
No Matches
thpool.h File Reference

this file Defines everything related to ThreadPool More...

Go to the source code of this file.

Data Structures

struct  SNF_ThreadPool_work_t
 Defines the 'work' needed to do by defining a 'func'tion and it's 'arg'ument. More...
 
struct  SNF_ThreadPool_worker_t
 Defines the 'worker'that would handle a certain 'work'. More...
 
struct  SNF_ThreadPool_t
 The structure for a Thread Pool. More...
 

Typedefs

typedef struct SNF_ThreadPool_work_t SNF_thpool_work
 
typedef struct SNF_ThreadPool_worker_t SNF_thpool_worker
 
typedef struct SNF_ThreadPool_t SNF_thpool
 
typedef enum SNF_ThreadPool_Status_e SNF_thpool_status
 

Enumerations

enum  SNF_ThreadPool_Status_e { SNF_THPOOL_STOP , SNF_THPOOL_RUN , SNF_THPOOL_DESTROY }
 

Functions

int snf_thpool_inis (SNF_thpool **ThreadPool, int Max_Threads, void *(*Main_Worker)(void *), void *arg)
 Insitanciates a new thread pool, with a Limiter ( Max_Threads )
 
void snf_thpool_addwork (SNF_thpool *pool, void *(*func)(), void *arg)
 Creates a "work" that will call the func function with argument arg
 
void snf_thpool_wait (SNF_thpool *pool)
 Blocks current thread until the Thread Pool's last changed status has taken effect.
 
void snf_thpool_join (SNF_thpool *pool)
 Will Block till the Main_worker finishes.
 
void snf_thpool_stop (SNF_thpool *pool)
 will order the Thread Pool to stop.
 
void snf_thpool_run (SNF_thpool *pool)
 

Variables

time_t SNF_THPOOL_STOPWAIT
 This defines the amount of seconds to wait a worker to stop, if the worker does not stop during this time period it will be forcefully be stopped.
 

Detailed Description

this file Defines everything related to ThreadPool

Thread Pools By Abdelhadi Seddar This implemantation of a Threadpool is by no means perfect nor optimized, because I made it based of a graph depicting how a threadpool works, without seeing any documentation about it, this is just a learning experience for me and im learning through trial and error.

UPDATE 0.0.2-alpha: After research and trial and error, and feedback from other people and after trying it in a project, i realized that i made the implementation wrong, as i used to create each threads whenever there is a work to do, which added CPU Time and latency, so after Reading this Wikipedia Page (Last visited on Augest 2, 2024 at 2:37 PM GMT +1) I noticed that i should've created the threads At once and from the beggining. so i did just that in this update and it did do wonders after comparing the performance of the two (and doesnt crash as often as 0.0.1-alpha)

Typedef Documentation

◆ SNF_thpool_work

◆ SNF_thpool_worker

◆ SNF_thpool

typedef struct SNF_ThreadPool_t SNF_thpool

◆ SNF_thpool_status

Enumeration Type Documentation

◆ SNF_ThreadPool_Status_e

Enumerator
SNF_THPOOL_STOP 

Used to Signify that the Thread Pool was stopped or been ordred to.

Warning
: Only set this status using snf_thpool_stop
SNF_THPOOL_RUN 

Used to Signify that the Thread Pool is running or been ordred to.

Warning
: Only set this status using snf_thpool_run
SNF_THPOOL_DESTROY 

Used to Signify that the Thread Pool is being ordred to be destroyied.

Warning
Only Set this Value by calling snf_thpool_destroy

Function Documentation

◆ snf_thpool_inis()

int snf_thpool_inis ( SNF_thpool ** ThreadPool,
int Max_Threads,
void *(* Main_Worker )(void *),
void * arg )
extern

Insitanciates a new thread pool, with a Limiter ( Max_Threads )

Parameters
ThreadPoolReturns the thread pool by writes the newly created instance in the pointer given in this parameter
Max_ThreadsMaximum Total allowed threads ( Must be 2+ if No Main Worker given, else must me 3+ for proper work)
Main_WorkerMain Function that would be called upon finishing the intialisation of the
argthe argument that would be given to the Main_worker function.
Returns
0 On Success || -1 On fail { Shall fail only if 1 - ThreadPool is NULL || 2 - Max_Thread is below recommended/required value }

◆ snf_thpool_addwork()

void snf_thpool_addwork ( SNF_thpool * pool,
void *(* func )(),
void * arg )
extern

Creates a "work" that will call the func function with argument arg

Parameters
poolThe Thead Pool to be operated on.
funcThe function that would be called once a "worker" picks up the "work".
argargument to be given to the func upon call.

◆ snf_thpool_wait()

void snf_thpool_wait ( SNF_thpool * pool)
extern

Blocks current thread until the Thread Pool's last changed status has taken effect.

Parameters
poolThe Thead Pool to be operated on.

◆ snf_thpool_join()

void snf_thpool_join ( SNF_thpool * pool)
extern

Will Block till the Main_worker finishes.

Parameters
poolThe Thead Pool to be operated on.

◆ snf_thpool_stop()

void snf_thpool_stop ( SNF_thpool * pool)
extern

will order the Thread Pool to stop.

Note
you can block till the Thread Pool has stopped
Parameters
poolThe Thead Pool to be operated on.

◆ snf_thpool_run()

void snf_thpool_run ( SNF_thpool * pool)
extern

Variable Documentation

◆ SNF_THPOOL_STOPWAIT

time_t SNF_THPOOL_STOPWAIT
extern

This defines the amount of seconds to wait a worker to stop, if the worker does not stop during this time period it will be forcefully be stopped.

Note
Default Value is 1 Second