parallel_for_each.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. Copyright 2005-2013 Intel Corporation. All Rights Reserved.
  3. This file is part of Threading Building Blocks.
  4. Threading Building Blocks is free software; you can redistribute it
  5. and/or modify it under the terms of the GNU General Public License
  6. version 2 as published by the Free Software Foundation.
  7. Threading Building Blocks is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  9. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with Threading Building Blocks; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  14. As a special exception, you may use this file as part of a free software
  15. library without restriction. Specifically, if other files instantiate
  16. templates or use macros or inline functions from this file, or you compile
  17. this file and link it with other files to produce an executable, this
  18. file does not by itself cause the resulting executable to be covered by
  19. the GNU General Public License. This exception does not however
  20. invalidate any other reasons why the executable file might be covered by
  21. the GNU General Public License.
  22. */
  23. #ifndef __TBB_parallel_for_each_H
  24. #define __TBB_parallel_for_each_H
  25. #include "parallel_do.h"
  26. namespace tbb {
  27. //! @cond INTERNAL
  28. namespace internal {
  29. // The class calls user function in operator()
  30. template <typename Function, typename Iterator>
  31. class parallel_for_each_body : internal::no_assign {
  32. const Function &my_func;
  33. public:
  34. parallel_for_each_body(const Function &_func) : my_func(_func) {}
  35. parallel_for_each_body(const parallel_for_each_body<Function, Iterator> &_caller) : my_func(_caller.my_func) {}
  36. void operator() ( typename std::iterator_traits<Iterator>::reference value ) const {
  37. my_func(value);
  38. }
  39. };
  40. } // namespace internal
  41. //! @endcond
  42. /** \name parallel_for_each
  43. **/
  44. //@{
  45. //! Calls function f for all items from [first, last) interval using user-supplied context
  46. /** @ingroup algorithms */
  47. #if __TBB_TASK_GROUP_CONTEXT
  48. template<typename InputIterator, typename Function>
  49. void parallel_for_each(InputIterator first, InputIterator last, const Function& f, task_group_context &context) {
  50. internal::parallel_for_each_body<Function, InputIterator> body(f);
  51. tbb::parallel_do (first, last, body, context);
  52. }
  53. #endif /* __TBB_TASK_GROUP_CONTEXT */
  54. //! Uses default context
  55. template<typename InputIterator, typename Function>
  56. void parallel_for_each(InputIterator first, InputIterator last, const Function& f) {
  57. internal::parallel_for_each_body<Function, InputIterator> body(f);
  58. tbb::parallel_do (first, last, body);
  59. }
  60. //@}
  61. } // namespace
  62. #endif /* __TBB_parallel_for_each_H */