Shadow Network Framework  0.0.2-alpha
C Server Library for Shadow Network Framework
Loading...
Searching...
No Matches
About Thread Pools

◄ Previous: Tutorials

"In computer programming, a thread pool is a software design pattern for achieving concurrency of execution in a computer program. ... a thread pool maintains multiple threads waiting for tasks to be allocated for concurrent execution by the supervising program. By maintaining a pool of threads, the model increases performance and avoids latency in execution due to frequent creation and destruction of threads for short-lived tasks" (Source)

About this implementation of Thread Pools

Please note that this implementation of thread pools that I made from the ground up so it is ground to have bugs or issues, if you incounter any one of these issues please report them at the Github repository.

How to use

First of all you need to add the Following header to make use of the thread pool

#include <SNF/thpool.h>
this file Defines everything related to ThreadPool

Initializing the ThreadPool

For Initializing we require 4 parameters

  • A pointer to a SNF_thpool * Variable ( Aka a SNF_thpool ** pointer)
  • The Maximum allowed threads to run for the thread pool. (Must be > 2 or >3 if Main_Worker (Next parameter ) is not NULL ).
  • A Function to be called on a separate thread that would be called as a Main_Worker that would not be affected if you stop or run the threadpool . ( Value could be NULL if you don't require this )
  • An Argument to be passed to your Main_Worker Function ( Ignored if Main_Worker is NULL)

once we have prepared all those parameters we call the function snf_thpool_inis Everything will be started directly after initializing. Example Code:

#include <stdlib.h>
#include <SNF/thpool.h>
/// Include your headers
/// Variable to Store the Thread pool
SNF_thpool *ThreadPool;
/// Main Function to call alongside the threadpool on a separate thread .
void *MainFunction(void *Argument)
{
/// Function Content
}
int main()
{
// Do NOT allocate space for ThreadPool Variable
&ThreadPool,
/// Run a threadpool with a max 5 thread including the Thread Handler and the thread calling the Main_Worker, which leaves 3 threads for the Works you assign
5,
/// Function to be called
MainFunction,
/// I needed no argument to pass so i wrote NULL
NULL
);
..
}
The structure for a Thread Pool.
Definition thpool.h:69
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 )

Stopping/Running The ThreadPool

as of 0.0.2-alpha youare able to stop the Thread Pool and close the threads running in the background then Run it again by recreating the threads according to configuration. Note however it does not affect the Main_Worker

Note
We assume that
SNF_thpool *ThreadPool;
that is inititialized in the previous part is the ThreadPool instance we use for examples below.
Warning
Never Run if the thread is already running nor Stop it if is already stopped

To check if the ThreadPool you must see the ThreadPool->thpool_status and what it equates to from one of the statuses stated in SNF_thpool_status