zoukankan      html  css  js  c++  java
  • 并发快速排序的面向对象解决方案

    template<typename T>
    struct Sorter
    {
        struct ChunkToSort
        {
            std::list<T>               data;
            std::promise<std::list<T>> promise;
        };
        
        ThreadSafeStack<ChunkToSort> chunks;
        std::vector<std::thread>   threads;
        size_t const               maxThreadCount;
        std::atomic<bool>          endOfData;
        
        Sorter():
        maxThreadCount(std::thread::hardware_concurrency () - 1),
        endOfData(false)
        {}
        
        ~Sorter()
        {
            endOfData = true;
            for (size_t i = 0; i < threads.size(); ++i){
                threads[i].join();
            }
        }
        
        void trySortChunk()
        {
            auto chunkPtr = chunks.pop ();
            if (chunkPtr != nullptr){
                sortChunk(chunkPtr);
            }
        }
        
        std::list<T> sortData(std::list<T>& chunkData)
        {
            if (chunkData.empty ()){
                return chunkData;
            }
            
            std::list<T> result;
            result.splice (result.begin (), chunkData, chunkData.begin ());
            auto const& partitionVal = result.front();
            auto divideIterator = std::partition(chunkData.begin (),
                                                 chunkData.end (),
                                                 [&](T const& val)
                                                 {return val < partitionVal;});
            ChunkToSort newLowerChunk;
            newLowerChunk.data.splice (newLowerChunk.data.end (), chunkData,
                                       chunkData.begin (), divideIterator);
            auto newLower = newLowerChunk.promise.get_future ();
            chunks.push(std::move(newLowerChunk));
            if(threads.size () < maxThreadCount){
                threads.push_back (std::thread(&Sorter<T>::sortThread, this));
            }
            std::list<T> newHigher(sortData (chunkData));
            result.splice (result.end (), newHigher);
            while(newLower.wait_for(std::chrono::seconds(0)) !=
                  std::future_status::ready){
                trySortChunk ();
            }
            result.splice (result.begin (), newLower.get ());
            return result;
        }
        
        void sortChunk(std::shared_ptr<ChunkToSort> const& chunk)
        {
            chunk->promise.set_value (sortData (chunk->data));
        }
        
        void sortThread()
        {
            while(endOfData){
                trySortChunk();
                std::this_thread::yield();
            }
        }
    };
    
    template<typename T>
    std::list<T> parallelQuickSort(std::list<T>& input)
    {
        if (input.empty ()){
            return input;
        }
        
        Sorter<T> sorter;
        return sorter.sortData (input);
    }
  • 相关阅读:
    Fuck,什么破书
    数组指针与指针数组的问题
    .....不敢私藏,魂淡啊...游戏程序员们懂的
    毕业设计做个3D赛车游戏
    原来是这样的,所噶
    出来混,迟早都是要还的
    Struts2源码学习DefaultConfiguration的RuntimeConfigurationImpl方法(运行期改造)
    Struts2中拦截器实现AOP的原理分析
    人生的两个方向:一个是出门,一个是回家(转载)
    Struts 2框架结构图
  • 原文地址:https://www.cnblogs.com/wuOverflow/p/4887133.html
Copyright © 2011-2022 走看看