File Transfer Project module 1-thread pool

Source: Internet
Author: User

#ifndef threadpool_h#define threadpool_h#include<iostream> #include <list> #include <exception># include<pthread.h> #include "locker.h"//thread mutex and synchronization mechanism using namespace Std;template<typename T>class THREADPOOL{PUBLIC://creates 8 threads by default, with a maximum request queue of 10000threadpool (int thread_number = 8, int max_requests = 10000); ~threadpool ();// Add a task to the request queue bool Append (T *request);p The work function of the rivate://thread, which constantly pulls the task out of the work queue and executes the static void *worker (void *arg); void run (); Private:int the number of threads in the m_thread_number;//thread pool int m_max_requests;//The maximum number of requests that the request queue can allow pthread_t m_threads;//describes the array of thread pools Same size and number of threads list<t *> m_workqueue;//request Work queue (first served) Locker m_queuelocker;//protection request queue Mutex SEM m_queuestat;//semaphore Determine if there is a task to handle whether the bool m_stop;//thread pool is freed};template<typename t> threadpool<t>::threadpool (int thread_number, int max_requests): M_thread_number (Thread_number), m_max_requests (max_requests), M_stop (false), M_threads (NULL) {if ( (thread_number <= 0) | | (max_requests<=0)) {throw std::exception ();} Create an array to hold the thread number m_threads = new Pthread_t[m_threaD_number];if (!m_threads) {throw std::exception ();} Create Thread_number threads and set them to detach from thread for (int i=0; i<thread_number; ++i) {cout<< "create" <<i+1<< "th Thread "<<endl;if (Pthread_create (M_thread[i], NULL, worker, this)! = 0)//The This pointer is passed in to make it easy to call This->worker (); Improved efficiency {Delete []m_threads;throw std::exception ();} Sets the thread state to non-blocking and returns immediately after the end similar to Waitpidif (Pthread_detack (M_threads[i])) {delete [] m_threads;throw std::exception ();}} Template<typename t> Threadpool<t>::~threadpool () {delete [] M_threads;m_stop = true;} Template<typename t> bool Threadpool<t>::append (T *request) {//Because the work queue is shared by multiple threads, there may be multiple threads concurrently processing the work queue,// Therefore, in the work queue related operations, must be locked m_queuelocker.lock ();//Locking if (m_workqueue.size () > M_max_requests) {m_queuelocker.unlock (); return false;} M_workqueue.push_back (request); M_queuelocker.unlock ();//Unlock m_queuestat.post () after operation completes;//post operation for semaphore return true; Template<typename t> void * Threadpool<t>::worker (void *arg) {ThreadPool *pool = (ThreadPool *) arg;// Get this pointer poOl->run ();//call run to process return pool;} Template<typename t> void Threadpool<t>::run () {while (!m_stop)//{m_queuestat.wait ();//wait for Semaphore m_ Queuelocker.lock ();//After the semaphore is obtained, the lock if (M_workqueue.empty ())//If the request Task force is listed as empty to unlock the next loop to continue polling {m_queuelocker.unlock (); Continue;} T *request = M_workqueue.front ();//Gets the request to be processed m_workqueue.push_front ();//The request is out of the team M_queuelocker.unlock ();//The queue operation is finished unlocking if (! Request)//requests to be empty for the next loop to continue polling {continue;} Request->process ();//real Request handling function!!!}} #endif

File Transfer Project module 1-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.