parallel_quick_sort.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright (C) 2012-2013 Vicente Botet
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #include <boost/config.hpp>
  6. #define BOOST_THREAD_VERSION 4
  7. #define BOOST_THREAD_PROVIDES_EXECUTORS
  8. #define BOOST_THREAD_USES_LOG_THREAD_ID
  9. #define BOOST_THREAD_QUEUE_DEPRECATE_OLD
  10. #if ! defined BOOST_NO_CXX11_DECLTYPE
  11. #define BOOST_RESULT_OF_USE_DECLTYPE
  12. #endif
  13. #include <boost/thread/executors/basic_thread_pool.hpp>
  14. #include <boost/thread/future.hpp>
  15. #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  16. #include <numeric>
  17. #include <algorithm>
  18. #include <functional>
  19. #include <iostream>
  20. #include <list>
  21. template<typename T>
  22. struct sorter
  23. {
  24. boost::basic_thread_pool pool;
  25. typedef std::list<T> return_type;
  26. std::list<T> do_sort(std::list<T> chunk_data)
  27. {
  28. if(chunk_data.empty())
  29. {
  30. return chunk_data;
  31. }
  32. std::list<T> result;
  33. result.splice(result.begin(),chunk_data, chunk_data.begin());
  34. T const& partition_val=*result.begin();
  35. typename std::list<T>::iterator divide_point=
  36. std::partition(chunk_data.begin(), chunk_data.end(), [&](T const& val){return val<partition_val;});
  37. std::list<T> new_lower_chunk;
  38. new_lower_chunk.splice(new_lower_chunk.end(), chunk_data, chunk_data.begin(), divide_point);
  39. boost::future<std::list<T> > new_lower = boost::async(pool, &sorter::do_sort, this, std::move(new_lower_chunk));
  40. //boost::future<std::list<T> > new_lower = boost::async<return_type>(pool, &sorter::do_sort, this, std::move(new_lower_chunk));
  41. std::list<T> new_higher(do_sort(chunk_data));
  42. result.splice(result.end(),new_higher);
  43. while(!new_lower.is_ready())
  44. {
  45. pool.schedule_one_or_yield();
  46. }
  47. result.splice(result.begin(),new_lower.get());
  48. return result;
  49. }
  50. };
  51. template<typename T>
  52. std::list<T> parallel_quick_sort(std::list<T>& input)
  53. {
  54. if(input.empty())
  55. {
  56. return input;
  57. }
  58. sorter<T> s;
  59. return s.do_sort(input);
  60. }
  61. int main()
  62. {
  63. try
  64. {
  65. const int s = 101;
  66. std::list<int> lst;
  67. for (int i=0; i<s;++i)
  68. lst.push_back(100-i);
  69. std::list<int> r = parallel_quick_sort(lst);
  70. for (std::list<int>::const_iterator it=r.begin(); it != r.end(); ++it)
  71. std::cout << *it << std::endl;
  72. }
  73. catch (std::exception& ex)
  74. {
  75. std::cout << "ERROR= " << ex.what() << "" << std::endl;
  76. return 1;
  77. }
  78. catch (...)
  79. {
  80. std::cout << " ERROR= exception thrown" << std::endl;
  81. return 2;
  82. }
  83. return 0;
  84. }
  85. #else
  86. //#warning "This compiler doesn't supports variadics and move semantics"
  87. int main()
  88. {
  89. return 0;
  90. }
  91. #endif