码迷,mamicode.com
首页 > 编程语言 > 详细

linux线程池

时间:2015-04-25 14:52:57      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

typedef struct task_queue 
{
  pthread_mutex_t mutex;
  pthread_cond_t cond;          /* when no task, the manager thread wait for ;when a new task come, signal. */
  struct task_node *head;       /* point to the task_link. */
  int number;                   /* current number of task, include unassinged and assigned but no finished. */
} TASK_QUEUE_T;


/*
*the ds of every thread, make all thread in a double link queue.
*/ 
typedef struct pthread_node 
{
  pthread_t tid;               /* the pid of this thread in kernel,the value is  syscall return . */
  int flag;                    /*  1:busy, 0:free. */
  struct task_node *work;      /*  if exec a work, which work. */
  struct pthread_node *next;
  struct pthread_node *prev
  pthread_cond_t cond;        /* when assigned a task, signal this child thread by manager. */
  pthread_mutex_t mutex; 
} THREAD_NODE;


/*
*the ds of the thread queue
*/ 
typedef struct pthread_queue 
{
  int number;                  /* the number of thread in this queue. */
  struct pthread_node *head;
  struct pthread_node *rear;
  pthread_cond_t cond;        /* when no idle thread, the manager wait for ,or when 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;

 

四大结构:

1、任务池struct,用于任务的管理,其中的head表示任务队列的第一个元素。内部的cond和mutex用于多线程操作此任务池

2、任务结点task_node,对应于相应的任务,这个任务结点中,包含任务对应的要执行的函数,完成此任务对应的线程id(用于找到线程),任务分配与否的标志flag,以及操作此任务node的mutex和connd

3、线程池struct pthread_queue,用于管理线程,有线程的首指针和尾指针,线程的个数number,线程池在代码中包括空闲线程和工作线程两大类,分别用两个变量表示

4、线程struc pthread_node,对应线程。包括线程的id,工作状态,此线程要运行的任务task_node指针,用于找到任务,然后 struct pthread_node *next;struct pthread_node *prev形成一个线程结构的双向链表

 

linux线程池

标签:

原文地址:http://www.cnblogs.com/kkshaq/p/4455844.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!