Linux thread pool

Source: Internet
Author: User

typedefstructTask_queue {pthread_mutex_t mutex;          pthread_cond_t cond; /*When no task, the manager thread is wait for; When a new task come, signal.*/  structTask_node *head;/*Point to the Task_link.*/  intNumber/*Current number of tasks, include unassinged and assigned but no finished.*/} task_queue_t;/**the ds of every thread, make all the thread in a double link queue.*/typedefstructPthread_node {pthread_t tid; /*The PID of this thread in kernel,the value is Syscall return.*/  intFlag/*1:busy, 0:free.*/  structTask_node *work;/*if exec a work, which work.*/  structPthread_node *Next; structPthread_node *prev pthread_cond_t cond; /*When assigned a task, signal the child thread by manager.*/pthread_mutex_t Mutex;} Thread_node;/**the ds of the thread queue*/typedefstructPthread_queue {intNumber/*The number of the thread in this queue.*/  structPthread_node *Head; structPthread_node *Rear;        pthread_cond_t cond; /*When no idle thread, the manager is wait for, or if a thread return with idle, signal.*/pthread_mutex_t Mutex;} pthread_queue_t;

typedef struct TASK_NODE
{
void *arg; /* Fun ARG. * *
void * (*fun) (void *); /* The real work of the task. */
pthread_t Tid; /* which thread exec this task. */
int work_id; /* task ID. */
int flag; /* 1:assigned, 0:unassigned. */
struct Task_node *next;
pthread_mutex_t Mutex; /* When modify this DS and exec the Work,lock the task DS. */
} Task_node;

Four structure:

1. Task pool struct, which is used for task management, where head represents the first element of a task queue. Internal cond and mutexes for multi-threaded operations for this task pool

2, the Task node Task_node, corresponding to the corresponding task, this task node, contains the task corresponding to the function to be executed, complete the task corresponding to the thread ID (used to find the thread), the assignment of the task flag flag, and operation of this task node's mutex and connd

3, thread pool struct pthread_queue, used to manage threads, the first pointer and the tail pointer of thread, number of threads, line pool includes idle thread and worker thread in code, which are represented by two variables respectively.

4, Thread Struc pthread_node, corresponding thread. Includes the ID of the thread, the working state, the task Task_node pointer to run by this thread, the task to find, and then the struct Pthread_node *next; struct Pthread_node *prev form a doubly linked list of threaded structures

Linux thread pool

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.