协能can协议
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

MThreadedJobQImpl.h 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * MThreadedJobQImpl.h
  3. *
  4. * Created on: 2016年11月18日
  5. * Author: nego
  6. */
  7. #ifndef SERVICE_IMPL_MTHREADEDJOBQIMPL_H_
  8. #define SERVICE_IMPL_MTHREADEDJOBQIMPL_H_
  9. #include <pthread.h>
  10. #include <vector>
  11. class JobExecuterImpl;
  12. class JobI;
  13. class MThreadedJobQImpl {
  14. public:
  15. typedef struct THNODE {
  16. //Pointer to the Job Executer
  17. JobExecuterImpl* pExecuter;
  18. //Pointer to next THNODE
  19. THNODE * pNext;
  20. } THNODE;
  21. //This is the pointer to the the thread (instance of CWinThread) which is
  22. //responsible for observing and assigning the jobs to the free executer threads.
  23. pthread_t pid;
  24. //CriticalSection object, used for synchronized access of shared resources
  25. //between the thread.
  26. //CCriticalSection m_cs;
  27. //This is the link list to maintain the priority queue of jobs.
  28. std::vector<JobI* > m_jobQList;
  29. //This method deletes a job executer. This method is not required to be used by the user. This is only for the use of the implementation of the multithreaded job queue.
  30. void deleteJobExecuter(JobExecuterImpl *pEx);
  31. //The setMaxNoOfExecuter method sets the maximum number of executer. If the user increases
  32. //the maximum number of executer then the multithreaded job queue will create more
  33. //threads to execute more jobs parallely. If the user decreases the max number of
  34. //executers then the job queue brings down the number of executer. The bringing
  35. //down process may not be immediate if all the executers are busy in processing.
  36. //In such a case the number will come down as soon as some jobs gets finished.
  37. void setMaxNoOfExecuter(int value);
  38. //This method adds a CJobExecuter to the Job executer link list. This method
  39. //is not required to be called by the user, this is only for the use of the
  40. //implementation of the multithreaded job queue
  41. void addJobExecuter(JobExecuterImpl *pEx);
  42. //This method returns a free Job Executer. This method is not required to be
  43. //called or used by the user. This is only for the implementation of the
  44. //multithreaded job queue.
  45. JobExecuterImpl* getJobExecuter();
  46. //This method adds a Job Executer to the free job executer list. This method
  47. //is not required to be used by the user, this is only for the use of the
  48. //implementation of the multithreaded job queue.
  49. void addFreeJobExecuter(JobExecuterImpl *pEx);
  50. //This method adds a job to the job queue. The job is passed as the parameter.
  51. void addJob(JobI *pJob);
  52. //This method returns the maximum number allowed executer threads.
  53. int getMaxNoOfExecuter();
  54. //This method returns the number of executer present at any instance of time.
  55. int getNoOfExecuter();
  56. //This is the static thread function. This function is not to be used by the user.
  57. //This function is used to create the observer thread.
  58. static void* JobObserverThreadFunction(void*);
  59. //This pauses the Multithreaded job queue, so that no new job gets processed.
  60. //But the user can add new jobs to the job queue. The jobs will be processed
  61. //when the user again resumes. But this does not pauses currently running jobs.
  62. void pause();
  63. //This resumes the Multithreaded job queue, after resume all the pending
  64. //jobs will be processed.
  65. void resume();
  66. void start();
  67. MThreadedJobQImpl();virtual ~MThreadedJobQImpl();
  68. private:
  69. //The BOOL attribute to state whether the observer thread should process or
  70. //not the new jobs.
  71. bool m_pause;
  72. //This int attribute represent the maximum limit of the executers. At any
  73. //instance of time the total number of free , and busy executer threads all
  74. //together should not exceed this number.
  75. int m_MaxNoOfExecuter;
  76. //This int attribute is to maintain the number of executer that are present
  77. //at a particular instance of time to execute the Jobs.
  78. int m_NoOfExecuter;
  79. //This is the pointer to the head of the link list which maintains the free executer
  80. //thread list.
  81. THNODE* m_pFreeEList;
  82. //This is the head of the link list that maintains all the executer thread list.
  83. THNODE* m_pAllEList;
  84. static pthread_mutex_t xLock;
  85. };
  86. #endif /* SERVICE_IMPL_MTHREADEDJOBQIMPL_H_ */