Commondef.h
Copy Code code as follows:
Unit seconds, monitoring the idle list time interval, the task that exceeds Task_destroy_interval time in the idle queue will be automatically destroyed
const int check_idle_task_interval = 300;
Unit seconds, Task automatic destroy time interval
const int task_destroy_interval = 60;
Monitor thread pool for empty intervals, microseconds
const int idle_check_poll_empty = 500;
Thread pool threads idle automatic exit time interval, 5 minutes
const int thread_wait_time_out = 300;
Taskpool.cpp
Copy Code code as follows:
#include "taskpool.h"
#include <string.h>
#include <stdio.h>
#include <pthread.h>
Taskpool::taskpool (const int & poolmaxsize)
: M_poolsize (Poolmaxsize)
, M_tasklistsize (0)
, M_bstop (False)
{
Pthread_mutex_init (&m_lock, NULL);
Pthread_mutex_init (&m_idlemutex, NULL);
Pthread_cond_init (&m_idlecond, NULL);
pthread_attr_t attr;
Pthread_attr_init (&ATTR);
Pthread_attr_setdetachstate (&attr, pthread_create_joinable); Let the thread run independently
Pthread_create (&m_idleid, &attr, Checkidletask, this); To create a monitoring idle task process
Pthread_attr_destroy (&ATTR);
}
Taskpool::~taskpool ()
{
if (!m_bstop)
{
Stoppool ();
}
if (!m_tasklist.empty ())
{
Std::list<task*>::iterator it = M_tasklist.begin ();
for (; it!= m_tasklist.end (); ++it)
{
if (*it!= NULL)
{
Delete *it;
*it = NULL;
}
}
M_tasklist.clear ();
m_tasklistsize = 0;
}
if (!m_idlelist.empty ())
{
Std::list<task*>::iterator it = M_idlelist.begin ();
for (; it!= m_idlelist.end (); ++it)
{
if (*it!= NULL)
{
Delete *it;
*it = NULL;
}
}
M_idlelist.clear ();
}
Pthread_mutex_destroy (&m_lock);
Pthread_mutex_destroy (&m_idlemutex);
Pthread_cond_destroy (&m_idlecond);
}
void * Taskpool::checkidletask (void * arg)
{
taskpool * pool = (taskpool*) arg;
while (1)
{
pool-> Lockidle ();
pool->removeidletask ();
if (Pool->getstop ())
{
pool->unlockidle ();
break;
}
pool->checkidlewait ();
Pool->unlockidle ();
}
}
void Taskpool::stoppool ()
{
M_bstop = true;
Lockidle ();
Pthread_cond_signal (&m_idlecond); Prevent the monitoring thread from waiting, causing a problem that cannot be exited
Unlockidle ();
Pthread_join (M_idleid, NULL);
}
BOOL Taskpool::getstop ()
{
return m_bstop;
}
void Taskpool::checkidlewait ()
{
struct TIMESPEC timeout;
memset (&timeout, 0, sizeof (timeout));
Timeout.tv_sec = Time (0) + check_idle_task_interval;
timeout.tv_nsec = 0;
Pthread_cond_timedwait (&m_idlecond, &m_idlemutex, &timeout);
}
int Taskpool::removeidletask ()
{
int iRet = 0;
Std::list<task*>::iterator it, next;
Std::list<task*>::reverse_iterator rit = M_idlelist.rbegin ();
time_t curtime = time (0);
for (; RIT!= m_idlelist.rend ();)
{
it =--rit.base ();
if (Difftime (Curtime (*it)->last_time)) >= task_destroy_interval)
{
iret++;
Delete *it;
*it = NULL;
Next = M_idlelist.erase (it);
RIT = Std::list<task*>::reverse_iterator (next);
}
Else
{
Break
}
}
}
int Taskpool::addtask (task_fun fun, void *arg)
{
int iRet = 0;
if (0!= fun)
{
Pthread_mutex_lock (&m_lock);
if (m_tasklistsize >= m_poolsize)
{
Pthread_mutex_unlock (&m_lock);
IRet =-1; Task pool is full;
}
Else
{
Pthread_mutex_unlock (&m_lock);
Task * task = Getidletask ();
if (NULL = Task)
{
task = new Task;
}
if (NULL = Task)
{
IRet =-2; New failed
}
Else
{
Task->fun = fun;
Task->data = arg;
Pthread_mutex_lock (&m_lock);
M_tasklist.push_back (Task);
++m_tasklistsize;
Pthread_mutex_unlock (&m_lock);
}
}
}
return iRet;
}
task* Taskpool::gettask ()
{
Task *task = NULL;
Pthread_mutex_lock (&m_lock);
if (!m_tasklist.empty ())
{
task = M_tasklist.front ();
M_tasklist.pop_front ();
--m_tasklistsize;
}
Pthread_mutex_unlock (&m_lock);
return task;
}
void Taskpool::lockidle ()
{
Pthread_mutex_lock (&m_idlemutex);
}
void Taskpool::unlockidle ()
{
Pthread_mutex_unlock (&m_idlemutex);
}
Task * Taskpool::getidletask ()
{
Lockidle ();
Task * task = NULL;
if (!m_idlelist.empty ())
{
task = M_idlelist.front ();
M_idlelist.pop_front ();
}
Unlockidle ();
return task;
}
void Taskpool::saveidletask (Task*task)
{
if (NULL!= Task)
{
Task->fun = 0;
Task->data = NULL;
Task->last_time = time (0);
Lockidle ();
M_idlelist.push_front (Task);
Unlockidle ();
}
}
Taskpool.h
Copy Code code as follows:
#ifndef Taskpool_h
#define Taskpool_h
/* Purpose @ Task pool, mainly buffer the number of external high concurrent tasks, the manager is responsible for scheduling tasks
* Task pool can automatically destroy long idle task objects
* IDLE Idle Process rotation Wait time can be checked by check_idle_task_interval setting
* Task_destroy_interval Set TASK idle time, exceeding this time value will be destroyed by Checkidletask thread
* Date @ 2013.12.23
* Author @ haibin.wang
*/
#include <list>
#include <pthread.h>
#include "commondef.h"
All the user actions are a task,
typedef void (*task_fun) (void *);
struct Task
{
Task_fun fun; Task handler function
void* data; Task processing data
time_t Last_time; Time to add an idle queue for automatic destruction
};
Task pool, all tasks are posted to the task pool, and the management thread is responsible for posting the task to the thread pool
Class Taskpool
{
Public
/* PUR-@ Initialization task pool, start Task Pool idle queue automatic destroy thread
* Para @ maxSize Maximum number of tasks, greater than 0
*/
Taskpool (const int & poolmaxsize);
~taskpool ();
/* Pur @ Add task to tail of task queue
* Para @ task, specific tasks
* Return @ 0 added successfully, negative number added failed
*/
int AddTask (task_fun fun, void* Arg);
* Pur @ Get a task from the head of the task list
* Return @ If there are tasks in the list, returns a task pointer, otherwise returns a null
*/
Task* Gettask ();
* Pur @ Save the Idle task to the free queue
* Para @ task has been invoked to perform tasks
* Return @
*/
void Saveidletask (Task*task);
void Stoppool ();
Public
void Lockidle ();
void Unlockidle ();
void Checkidlewait ();
int Removeidletask ();
BOOL Getstop ();
Private
static void * Checkidletask (void *);
/* Pur @ Get the idle task
* Para @
* Para @
* Return @ NULL description is not idle, otherwise get one from m_idlelist
*/
Task* Getidletask ();
int gettasksize ();
Private
int m_poolsize; Task Pool Size
int m_tasklistsize; Statistics the size of the tasklist, because when the size of the list increases with the number of
BOOL M_bstop; Whether to stop
Std::list<task*> m_tasklist;//all pending Tasks list
Std::list<task*> m_idlelist;//all idle Tasks list
pthread_mutex_t M_lock; Lock the task list to ensure that only one task is taken at a time
pthread_mutex_t M_idlemutex; Idle Task Queue Lock
pthread_cond_t M_idlecond; Idle queue wait Condition
pthread_t M_idleid;;
};
#endif
Threadpool.cpp
Copy Code code as follows:
/* Purpose @ Thread pool class, responsible for thread creation and destruction, realization line blocks until those automatic exit function (half resident)
* Date @ 2014.01.03
* Author @ haibin.wang
*/
#include "threadpool.h"
#include <errno.h>
#include <string.h>
/*
#include <iostream>
#include <stdio.h>
*/
Thread::thread (bool detach, ThreadPool * pool)
: M_pool (Pool)
{
Pthread_attr_init (&M_ATTR);
if (detach)
{
Pthread_attr_setdetachstate (&m_attr, pthread_create_detached); Let the thread run independently
}
Else
{
Pthread_attr_setdetachstate (&m_attr, pthread_create_joinable);
}
Pthread_mutex_init (&m_mutex, NULL); Initialize Mutex
Pthread_cond_init (&m_cond, NULL); Initializing a condition variable
Task.fun = 0;
Task.data = NULL;
}
Thread::~thread ()
{
Pthread_cond_destroy (&m_cond);
Pthread_mutex_destroy (&m_mutex);
Pthread_attr_destroy (&M_ATTR);
}
Threadpool::threadpool ()
: M_poolmax (0)
, M_idlenum (0)
, M_totalnum (0)
, M_bstop (False)
{
Pthread_mutex_init (&m_mutex, NULL);
Pthread_mutex_init (&m_runmutex,null);
Pthread_mutex_init (&m_terminalmutex, NULL);
Pthread_cond_init (&m_terminalcond, NULL);
Pthread_cond_init (&m_emptycond, NULL);
}
Threadpool::~threadpool ()
{
/*if (!m_threads.empty ())
{
Std::list<thread*>::iterator it = M_threads.begin ();
for (; it!= m_threads.end (); ++it)
{
if (*it!= NULL)
{
Pthread_cond_destroy (& ((*it)->m_cond));
Pthread_mutex_destroy (& ((*it)->m_mutex));
Delete *it;
*it = NULL;
}
}
M_threads.clear ();
}*/
Pthread_mutex_destroy (&m_runmutex);
Pthread_mutex_destroy (&m_terminalmutex);
Pthread_mutex_destroy (&m_mutex);
Pthread_cond_destroy (&m_terminalcond);
Pthread_cond_destroy (&m_emptycond);
}
int Threadpool::initpool (const int & poolmax, const int & poolpre)
{
if (Poolmax < Poolpre
|| Poolpre < 0
|| Poolmax <= 0)
{
return-1;
}
M_poolmax = Poolmax;
int iRet = 0;
for (int i=0; i<poolpre; ++i)
{
Thread * thread = CreateThread ();
if (NULL = thread)
{
IRet =-2;
}
}
if (IRet < 0)
{
Std::list<thread*>::iterator it = M_threads.begin ();
for (; it!= m_threads.end (); ++it)
{
if (NULL!= (*it))
{
Delete *it;
*it = NULL;
}
}
M_threads.clear ();
M_totalnum = 0;
}
return iRet;
}
void Threadpool::getthreadrun (Task_fun fun, void* Arg)
{
Get a thread from the thread pool
Pthread_mutex_lock (&m_mutex);
if (M_threads.empty ())
{
Pthread_cond_wait (&m_emptycond,&m_mutex); Blocking wait with idle thread
}
Thread * thread = M_threads.front ();
M_threads.pop_front ();
Pthread_mutex_unlock (&m_mutex);
Pthread_mutex_lock (&thread->m_mutex);
Thread->task.fun = fun;
Thread->task.data = arg;
Pthread_cond_signal (&thread->m_cond); Trigger thread Wapperfun Loop execution
Pthread_mutex_unlock (&thread->m_mutex);
}
int Threadpool::run (task_fun fun, void * arg)
{
Pthread_mutex_lock (&m_runmutex); Guaranteed to be executed by only one thread at a time
int iRet = 0;
if (M_totalnum <m_poolmax)//
{
if (M_threads.empty () && (NULL = CreateThread ()))
{
IRet = -1;//can not create new thread!
}
Else
{
Getthreadrun (fun, ARG);
}
}
Else
{
Getthreadrun (fun, ARG);
}
Pthread_mutex_unlock (&m_runmutex);
return iRet;
}
void Threadpool::stoppool (bool bstop)
{
M_bstop = bstop;
if (bstop)
{
Start a thread that monitors whether all idle threads are exiting
Thread thread (false, this);
Pthread_create (&thread.m_threadid,&thread.m_attr, Threadpool::terminalcheck, &thread); Start monitoring all threads exiting threads
Blocking waits for all idle threads to exit
Pthread_join (Thread.m_threadid, NULL);
}
/*if (bstop)
{
Pthread_mutex_lock (&m_terminalmutex);
Start a thread that monitors whether all idle threads are exiting
Thread thread (true, this);
Pthread_create (&thread.m_threadid,&thread.m_attr, Threadpool::terminalcheck, &thread); Start monitoring all threads exiting threads
Blocking waits for all idle threads to exit
Pthread_cond_wait (&m_terminalcond, & M_terminalmutex);
Pthread_mutex_unlock (&m_terminalmutex);
}*/
}
BOOL Threadpool::getstop ()
{
return m_bstop;
}
Thread * Threadpool::createthread ()
{
thread * thread = NULL;
thread = new Thread (true, this);
if (NULL!= thread)
{
int Iret = Pthread_create (&thread->m_threadid,&thread->m_attr, Threadpool::wapperfun, thread); Add a thread to the idle queue by Wapperfun
if (0!= iret)
{
delete thread;
thread = NULL;
}
}
return thread;
}
void * Threadpool::wapperfun (VOID*ARG)
{
Thread * thread = (thread*) arg;
if (NULL = Thread | | NULL = = Thread->m_pool)
{
return NULL;
}
ThreadPool * Pool = thread->m_pool;
Pool->increasetotalnum ();
struct Timespec abstime;
memset (&abstime, 0, sizeof (abstime));
while (1)
{
if (0!= thread->task.fun)
{
Thread->task.fun (Thread->task.data);
}
if (true = = Pool->getstop ())
{
Break Determine whether to exit the thread after the current task has finished executing
}
Pthread_mutex_lock (&thread->m_mutex);
Pool->saveidlethread (thread); To add a thread to the idle queue
Abstime.tv_sec = Time (0) + thread_wait_time_out;
abstime.tv_nsec = 0;
if (etimedout = = pthread_cond_timedwait (&thread->m_cond, &thread->m_mutex, &abstime))//wait for thread to be awakened or timeout automatically exits
{
Pthread_mutex_unlock (&thread->m_mutex);
Break
}
Pthread_mutex_unlock (&thread->m_mutex);
}
Pool->lockmutex ();
Pool->decreasetotalnum ();
if (thread!= NULL)
{
Pool->removethread (thread);
Delete thread;
thread = NULL;
}
Pool->unlockmutex ();
return 0;
}
void Threadpool::saveidlethread (thread * thread)
{
if (thread)
{
Thread->task.fun = 0;
Thread->task.data = NULL;
Lockmutex ();
if (M_threads.empty ())
{
Pthread_cond_broadcast (&m_emptycond); Send an empty signal, tell the Run function thread queue is not empty
}
M_threads.push_front (thread);
Unlockmutex ();
}
}
int Threadpool::totalthreads ()
{
return m_totalnum;
}
void threadpool::sendsignal ()
{
Lockmutex ();
Std::list<thread*>::iterator it = M_threads.begin ();
for (; it!= m_threads.end (); ++it)
{
Pthread_mutex_lock (& (*it)->m_mutex);
pthread_cond_signal (& (*it)->m_cond));
Pthread_mutex_unlock (& (*it)->m_mutex);
}
Unlockmutex ();
}
void * Threadpool::terminalcheck (void* Arg)
{
Thread * thread = (thread*) arg;
if (NULL = Thread | | NULL = = Thread->m_pool)
{
return NULL;
}
ThreadPool * Pool = thread->m_pool;
while (false = = Pool->getstop ()) | | pool->totalthreads () >0)
{
Pool->sendsignal ();
Usleep (Idle_check_poll_empty);
}
Pool->terminalcondsignal ();
return 0;
}
void Threadpool::terminalcondsignal ()
{
Pthread_cond_signal (&m_terminalcond);
}
void Threadpool::removethread (thread* Thread)
{
M_threads.remove (thread);
}
void Threadpool::lockmutex ()
{
Pthread_mutex_lock (&m_mutex);
}
void Threadpool::unlockmutex ()
{
Pthread_mutex_unlock (&m_mutex);
}
void Threadpool::increasetotalnum ()
{
Lockmutex ();
m_totalnum++;
Unlockmutex ();
}
void ThreadPool::D ecreasetotalnum ()
{
m_totalnum--;
}
Threadpool.h
Copy Code code as follows:
#ifndef Threadpool_h
#define Threadpool_h
/* Purpose @ Thread pool class, responsible for thread creation and destruction, implementation line blocks until those automatic exit function (half resident) a
* Create a Terminalcheck thread when the thread pool exits, responsible for monitoring thread pool threads exiting
* Date @ 2013.12.23
* Author @ haibin.wang
*/
#include <list>
#include <string>
#include "taskpool.h"
Control task scheduling process by Threadmanager
ThreadPool's Terminalcheck thread is responsible for monitoring thread pool cable exit
Class ThreadPool;
Class Thread
{
Public
Thread (bool detach, ThreadPool * pool);
~thread ();
pthread_t M_threadid; Thread ID
pthread_mutex_t M_mutex; Mutual exclusion Lock
pthread_cond_t M_cond; Condition variable
pthread_attr_t m_attr; Thread Properties
Task task; //
ThreadPool * M_POOL; Owning thread pool
};
Thread pool, which is responsible for creating the threading task, after which the thread is added to the idle queue, from the task pool
Class ThreadPool
{
Public
ThreadPool ();
~threadpool ();
*/pur @ initialization thread pool
* Para @ Poolmax thread pool Maximum number of threads
* Para @ poolpre number of pre-created threads
* Return @ 0: Successful
* -1:parameter error, must poolmax > Poolpre >=0
*-2: Failed to create thread
*/
int Initpool (const int & poolmax, const int & poolpre);
/* PUR @ Perform a task
* Para @ Task task pointer
* Return @ 0 Task assignment succeeded, negative task assignment failed,-1, create new thread failed
*/
int Run (task_fun fun, void* Arg);
/* Pur @ set whether to stop thread pooling work
* Para @ bstop True stops, false does not stop
*/
void Stoppool (bool bstop);
Public://This common function is primarily used for static function calls
* Pur @ Get the start and stop state of the process pool
* Return @
*/
BOOL Getstop ();
void Saveidlethread (thread * thread);
void Lockmutex ();
void Unlockmutex ();
void Decreasetotalnum ();
void Increasetotalnum ();
void Removethread (thread* Thread);
void Terminalcondsignal ();
int totalthreads ();
void Sendsignal ();
Private
/* Pur @ Create thread
* Return @ non-null success, NULL failed,
*/
Thread * CreateThread ();
/* Pur @ get one thread from thread pool run task
* Para @ FUN function pointer
* Para @ ARG function argument
* Return @
*/
void Getthreadrun (Task_fun fun, void* Arg);
static void * Wapperfun (void*);
static void * Terminalcheck (void*);//loop monitor if all threads terminate threads
Private
int m_poolmax;//thread pool maximum number of threads
int m_idlenum; Number of idle threads
int m_totalnum; The current total number of threads is less than the maximum number of threads
BOOL M_bstop; Whether to stop the thread pool
pthread_mutex_t M_mutex; Thread List Lock
pthread_mutex_t M_runmutex; Run function lock
pthread_mutex_t M_terminalmutex; Terminate all Thread mutexes
pthread_cond_t M_terminalcond; Terminate all thread condition variables
pthread_cond_t M_emptycond; Idle thread non-empty condition variable
Std::list<thread*> m_threads; Thread List
};
#endif
Threadpoolmanager.cpp
Copy Code code as follows:
#include "Threadpoolmanager.h"
#include "threadpool.h"
#include "taskpool.h"
#include <errno.h>
#include <string.h>
/* #include <string.h>
#include <sys/time.h>
#include <stdio.h>*/
struct Timeval Time_beg, time_end;
Threadpoolmanager::threadpoolmanager ()
: M_threadpool (NULL)
, M_taskpool (NULL)
, M_bstop (False)
{
Pthread_mutex_init (&m_mutex_task,null);
Pthread_cond_init (&m_cond_task, NULL);
/* memset (&time_beg, 0, sizeof (struct timeval));
memset (&time_end, 0, sizeof (struct timeval));
Gettimeofday (&time_beg, NULL);
}
Threadpoolmanager::~threadpoolmanager ()
{
StopAll ();
if (NULL!= m_threadpool)
{
Delete M_threadpool;
M_threadpool = NULL;
}
if (NULL!= m_taskpool)
{
Delete M_taskpool;
M_taskpool = NULL;
}
Pthread_cond_destroy (&m_cond_task);
Pthread_mutex_destroy (&m_mutex_task);
/*gettimeofday (&time_end, NULL);
Long total = (time_end.tv_sec-time_beg.tv_sec) *1000000 + (TIME_END.TV_USEC-TIME_BEG.TV_USEC);
printf ("Manager Total time =%d\n", total);
gettimeofday (&time_beg, NULL); */
}
Int Threadpoolmanager::init (
const int &tastpoolsize,
const int &threadpoolmax,
const int &threadpoolpre)
{
m_threadpool = new ThreadPool ();
if (NULL = = M_threadpool)
{
return-1;
}
m_taskpool = new Taskpool (tastpoolsize);
if (NULL = m_taskpool)
{
Return-2;
}
if (0>m_threadpool->initpool (Threadpoolmax, Threadpoolpre))
{
return-3;
}
/boot thread pool
/start Task Pool
/start task fetch thread, pull task from task pool to thread pool
pthread_attr_t attr;
Pthread_attr_init (&attr);
pthread_attr_setdetachstate (&attr, pthread_create_joinable);
pthread_create (&m_taskthreadid, &attr, Taskthread, this);//create Fetch task process
Pthread_attr_destroy (&attr);
return 0;
}
void Threadpoolmanager::stopall ()
{
M_bstop = true;
Locktask ();
Pthread_cond_signal (&m_cond_task);
Unlocktask ();
Pthread_join (M_taskthreadid, NULL);
Wait for all current tasks to complete
M_taskpool->stoppool ();
M_threadpool->stoppool (TRUE); Stop thread pool work
}
void Threadpoolmanager::locktask ()
{
Pthread_mutex_lock (&m_mutex_task);
}
void Threadpoolmanager::unlocktask ()
{
Pthread_mutex_unlock (&m_mutex_task);
}
void* Threadpoolmanager::taskthread (void* Arg)
{
Threadpoolmanager * manager = (threadpoolmanager*) arg;
while (1)
{
Manager->locktask (); Prevent the task from not executing and sending a stop signal
while (1)//After the tasks in the task queue have been executed and then exited
{
Task * task = Manager->gettaskpool ()->gettask ();
if (NULL = Task)
{
Break
}
Else
{
Manager->getthreadpool ()->run (Task->fun, task->data);
Manager->gettaskpool ()->saveidletask (Task);
}
}
if (Manager->getstop ())
{
Manager->unlocktask ();
Break
}
Manager->taskcondwait (); Wait for a task to execute
Manager->unlocktask ();
}
return 0;
}
ThreadPool * Threadpoolmanager::getthreadpool ()
{
return m_threadpool;
}
Taskpool * Threadpoolmanager::gettaskpool ()
{
return m_taskpool;
}
int threadpoolmanager::run (task_fun fun,void* Arg)
{
if (0 = fun)
{
return 0;
}
if (!m_bstop)
{
int IRet = m_taskpool->addtask (fun, arg);
if (IRet = 0 && (0 = pthread_mutex_trylock (&m_mutex_task)))
{
Pthread_cond_signal (&m_cond_task);
Unlocktask ();
}
return iRet;
}
Else
{
return-3;
}
}
BOOL Threadpoolmanager::getstop ()
{
return m_bstop;
}
void Threadpoolmanager::taskcondwait ()
{
struct TIMESPEC to;
memset (&to, 0, sizeof to);
To.tv_sec = Time (0) + 60;
to.tv_nsec = 0;
Pthread_cond_timedwait (&m_cond_task, &m_mutex_task, &to); 60 Seconds Timeout
}
Threadpoolmanager.h
Copy Code code as follows:
#ifndef Threadpoolmanager_h
#define Threadpoolmanager_h
* Purpose @
* Basic Process:
* Manage the thread pool and task pool, join the task pool, then the taskthread is responsible for removing the task from the pool of tasks and putting it into the thread pool
* Basic function:
* 1, the worker thread can automatically exit some long time unused thread when the business is not busy
* 2, Task pool can automatically release long time unused resources when the business is not busy (can be modified by commondef.h)
* 3, when the program is no longer to the task pool to add tasks, when the task pool all the tasks completed before exiting the relevant program (do the safe exit of the program)
* Thread Resources:
* If no processing thread is allocated, ThreadPool only actually creates the required threads when there is a task, the maximum number of threads created is user specified
* When manager destroys, Manager creates a monitoring thread that monitors the execution of all tasks, and the manager does not destroy until all tasks have been completed
* Maximum number of threads: 1 Taskpool Threads + 1 Manager Task Scheduler threads + ThreadPool Max Threads + 1 Manager Exit monitor thread + 1 thread pool thread exit monitor threads
* Thread Minimum number is: 1 Taskpool Create idle Task resource destroy monitor thread + 1 manager create task scheduling thread
* Use method:
* Threadpoolmanager Manager;
* Manager. Init (100000, 50, 5);//Initialize one task pool to 10000, thread pool max thread number 50, pre-Create 5-thread Manager
* Manager.run (fun, data); Add execute task to Manager, fun is function pointer, data is fun need to pass in parameter, data can be null
*
* Date @ 2013.12.23
* Author @ haibin.wang
*
* Detailed parameter control can modify the value of related variables in commondef.h
*/
#include <pthread.h>
typedef void (*task_fun) (void *);
Class ThreadPool;
Class Taskpool;
Class Threadpoolmanager
{
Public:
Threadpoolmanager ();
~threadpoolmanager ();
/* pur-initiated thread pool and task pool, Threadpoolmax > Threadpoolpre > Threadpoolmin >= 0
& nbsp; * para @ tastpoolsize task pool size
* para @ Threadpoolmax thread pool Maximum number of threads
* Para @ threadpoolpre number of pre-created threads
* Return @ 0: initialization succeeded, negative numbers failed to initialize
* -1: Create thread pool failure
* 2: Create Task pool failed
* 3: Thread pool initialization failed
*/
int Init (const int &tastpoolsize,
const int & Threadpoolmax,
const int &threadpoolpre );
/* Pur @ Perform a task
* Para @ FUN function pointers to execute
* PA RA @ arg fun required parameter, default to NULL
* Return @ 0 Task assignment succeeded, negative task assignment failed
* & nbsp; -1: Task Pool full
* -2: Task pool new failed
* -3:manager has sent a stop signal and is no longer receiving new tasks
*/
int Run (task_fun fun, void* arg=null);
Public://The following public function is used primarily for static function calls
bool Getstop ();
void taskcondwait ();
Taskpool * Gettaskpool ();
ThreadPool * Getthreadpool ();
void Locktask ();
void Unlocktask ();
void Lockfull ();
Private
static void * Taskthread (void*); Task Processing Threads
void StopAll ();
Private
ThreadPool *m_threadpool; Thread pool
Taskpool * M_TASKPOOL; Task Pool
BOOL M_bstop; Whether to terminate the manager
pthread_t M_taskthreadid; Taskthread thread ID
pthread_mutex_t M_mutex_task;
pthread_cond_t M_cond_task;
};
#endif
Main.cpp
Copy Code code as follows:
#include <iostream>
#include <string>
#include "Threadpoolmanager.h"
#include <sys/time.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
using namespace Std;
int seq = 0;
int billnum = 0;
int inter = 1;
pthread_mutex_t M_mutex;
void MyFunc (Void*arg)
{
Pthread_mutex_lock (&m_mutex);
seq++;
if (seq%inter = 0)
{
cout << "Fun 1=" << seq << Endl;
}
if (seq>=1000000000)
{
cout << "billion" << Endl;
seq = 0;
billnum++;
}
Pthread_mutex_unlock (&m_mutex);
Sleep ();
}
int main (int argc, char** argv)
{
if (argc!= 6)
{
cout << "must have 5 parameters task execution number task pool size thread pool size pre-create thread number output interval" << Endl;
cout << "eg:./test 999999 10000" << Endl;
cout << The example above represents creating an interval of 20 task outputs, a task pool size of 10000, a thread pool size of 100, a pre-created 10 thread, and the number of tasks performed: 999999 << Endl;
return 0;
}
Double loopsize = atof (argv[1]);
int tasksize = atoi (argv[2]);
int threadpoolsize = atoi (argv[3]);
int presize = atoi (argv[4]);
Inter = Atoi (argv[5]);
Pthread_mutex_init (&m_mutex,null);
Threadpoolmanager Manager;
if (0>manager. Init (Tasksize, ThreadPoolSize, presize))
{
cout << "Initialization failure" << Endl;
return 0;
}
cout << "******************* initialization complete *********************" << Endl;
struct Timeval Time_beg, time_end;
memset (&time_beg, 0, sizeof (struct timeval));
memset (&time_end, 0, sizeof (struct timeval));
Gettimeofday (&time_beg, NULL);
Double i=0;
for (; i<loopsize; ++i)
{
while (0>manager. Run (Myfunc,null))
{
Usleep (100);
}
}
Gettimeofday (&time_end, NULL);
Long total = (time_end.tv_sec-time_beg.tv_sec) *1000000 + (TIME_END.TV_USEC-TIME_BEG.TV_USEC);
cout << "Total time =" << total << Endl;
cout << "Total num =" << i << "billion num=" << billnum<< Endl;
cout << __file__ << "will close all threads" << Endl;
Pthread_mutex_destroy (&m_mutex);
return 0;
}