
What is Thread ?
A thread is a path of execution within a process and a process can contain multiple threads. For example, in a browser, multiple tabs can be different threads. MS Word uses multiple threads: one thread to format the text, another thread to process inputs, etc. They can either execute in User space or kernel space [sudo].
Linux does not distinguish between processes and threads – It uses the more generic term “tasks”.

Creating of Thread
Header files used for thread handling
#include <pthread.h>
/**********************************************************
* Author :- Aditya Gaurav *
* Mail :- adiitrack7@gmail.com *
* *
* 🙂 Visit and You know what to do www.errbits.com 🙂 *
*********************************************************/
/* @brief Create a new thread, starting with execution of
* START-ROUTINE getting passed ARG. Creation
* attributed come from ATTR. The new handle
* is stored in *NEWTHREAD.
* @param __newthread thread instance
* @param __attr attribute created for this variable
* @param A function pointer
* @param parameter passed to function pointer
* @return returns zero when the call completes
* successfully.
*/
extern int pthread_create (
pthread_t *__restrict __newthread,
const pthread_attr_t *__restrict __attr,
void *(*__start_routine) (void *),
void *__restrict __arg) __THROWNL __nonnull ((1, 3)
);
/* @brief Make calling thread wait for termination of the thread TH. The
* exit status of the thread is stored in *THREAD_RETURN, if THREAD_RETURN
* is not NULL.
* @param __th thread instance
* @param __thread_return pointer to return from thread
*/
extern int pthread_join (pthread_t __th, void **__thread_return);
Now one must have to configure what attributes we need to set to thread, attributes means what Scheduling policy you want to use [You know Round Robin Similarly others are also there ] ,Which CPU core you want to use or want to use all core along with priority of that thread. or You can pass NULL and let Kernel decide its execution path.
Setting up thread attributes
Data type that is used for setting up attributes is
/**
* @brief Attribute used along with thread creation
*/
pthread_attr_t fifo_sched_attr;
Three things we need to set
1. Scheduling Policy
2.Priority of Thread
3.Core of execution
Scheduling Policy
The scheduler is the kernel component that decides which runnable thread will be executed by the CPU next. Each thread has an associated scheduling policy and a static scheduling priority, sched_priority. The scheduler makes its decisions based on knowledge of the scheduling policy and static priority of all threads on the system.
/**
* 🙂 Visit www.errbits.com & Subscribe and support us 🙂
*
*@param pthread_attr_t attribute to setup policy
*@param __policy Type of policy which you want to assign
**/
pthread_attr_setschedpolicy(pthread_attr_t, __policy);
Priority of Thread
Processes scheduled under one of the real-time policies (SCHED_FIFO, SCHED_RR) have a sched_priority value in the range 1 (low) to 99 (high). (As the numbers imply, real-time threads always have higher priority than normal threads.)
/**
* 🙂 Visit www.errbits.com & Subscribe and support us 🙂
**/
//priority value
int max_prio, cpuidx;
struct sched_param fifo_param;
//get priority
max_prio=sched_get_priority_max(SCHED_POLICY);
fifo_param.sched_priority=max_prio;
//setting schedular with all details
pthread_attr_setschedparam(&fifo_sched_attr, &fifo_param);
Core of execution
/**
* 🙂 Subscribe and support us www.errbits.com 🙂
**/
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
//setting core id to use
cpuidx=(1);
CPU_SET(cpuidx, &cpuset);
//API to set cpu core with SCHED policy
pthread_attr_setaffinity_np(pthread_attr_t, sizeof(cpu_set_t), &cpuset);
For more details on CPU scheduling click here.

Leave a comment