ParallelJob.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #ifndef ANKI_CORE_PARALLEL_JOB_H
  2. #define ANKI_CORE_PARALLEL_JOB_H
  3. #include <boost/thread.hpp>
  4. namespace anki {
  5. class ParallelManager;
  6. class ParallelJob;
  7. /// The base class of the parameters the we pass in the job
  8. struct ParallelJobParameters
  9. {};
  10. /// The callback that we feed to the job
  11. typedef void (*ParallelJobCallback)(ParallelJobParameters&, const ParallelJob&);
  12. /// The thread that executes a ParallelJobCallback
  13. class ParallelJob
  14. {
  15. public:
  16. /// Constructor
  17. ParallelJob(int id, const ParallelManager& manager,
  18. boost::barrier& barrier);
  19. /// @name Accessors
  20. /// @{
  21. uint getId() const
  22. {
  23. return id;
  24. }
  25. const ParallelManager& getManager() const
  26. {
  27. return manager;
  28. }
  29. /// @}
  30. /// Assign new job to the thread
  31. void assignNewJob(ParallelJobCallback callback,
  32. ParallelJobParameters& jobParams);
  33. private:
  34. uint id; ///< An ID
  35. boost::thread thread; ///< Runs the workingFunc
  36. boost::mutex mutex; ///< Protect the ParallelJob::job
  37. boost::condition_variable condVar; ///< To wake up the thread
  38. boost::barrier& barrier; ///< For synchronization
  39. ParallelJobCallback callback; ///< Its NULL if there are no pending job
  40. ParallelJobParameters* params;
  41. /// Know your father and pass him to the jobs
  42. const ParallelManager& manager;
  43. /// Start thread
  44. void start()
  45. {
  46. thread = boost::thread(&ParallelJob::workingFunc, this);
  47. }
  48. /// Thread loop
  49. void workingFunc();
  50. };
  51. } // end namespace
  52. #endif