随笔分类 - 并发
摘要:class ThreadRAII { public: // whether join or detach should be called, // when a this object is destroyed. enum class DtorAction { join, detach }; Thr
阅读全文
摘要:std::mutex m; std::condition_variable cond; int flag = 0; constexpr int kLoopTimes = 10; void foo(int id){ for (int i = 0; i != kLoopTimes; ++i){ std:
阅读全文
摘要:templateIterator parallelFind(Iterator first, Iterator last, MatchType match){ struct FindElement { void operator()(Iterator begin, Itera...
阅读全文
摘要:class ThreadsJoiner{ std::vector& threads;public: ThreadsJoiner(std::vector& threads_): threads(threads_) {} ~ThreadsJoiner() ...
阅读全文
摘要:templateT parallelAccumulate(Iterator first, Iterator last, T init){ size_t const length = std::distance(first, last); if (length == 0){ ...
阅读全文
摘要:templatestruct AccumulateBlock{ T operator()(Iterator first, Iterator last) { return std::accumulate(first, last, T()); }};class Threa...
阅读全文
摘要:templatestruct Sorter{ struct ChunkToSort { std::list data; std::promise> promise; }; ThreadSafeStack chun...
阅读全文
摘要:templateclass ThreadSafeStack{private: std::stack data; mutable std::mutex m;public: ThreadSafeStack() = default; ThreadSafeStack(con...
阅读全文
摘要:templateclass LockFreeStack{private: struct Node; struct CountedNode { int externalCount = 0; Node* ptr = nullp...
阅读全文
摘要:constexpr size_t maxHazardPointers = 100;struct HazardPointer{ std::atomic id; std::atomic pointer;};array hazardPointers;class Hazard...
阅读全文
摘要:templateclass LockFreeStack{private: struct Node { std::shared_ptr data; Node* next; Node(T const& value): ...
阅读全文
摘要:templateclass ThreadsafeList{ struct Node { std::mutex m; std::shared_ptr data; std::unique_ptr next; Node(): ...
阅读全文
摘要:template>class ThreadsafeLookupTable{private: class BucketType { private: typedef std::pair bucketValue; typedef std::list buck...
阅读全文
摘要:templateclass ThreadsafeQueue{private: struct Node { std::shared_ptr data; std::unique_ptr next; }; std::unique_...
阅读全文
摘要:上一篇文章里说到了一个极简易队列的实现,然而它对于并发存在一个问题,就是当多个或者说就是两个线程并发地访问队列,分别调用 push() 与 tryPop() 时,可能就会导致数据争用或者死锁。以下是一种思路,通过分离数据允许并发。其大致思路是预先分配一个不储存任何数据的结点占位,当 push() 进...
阅读全文
摘要:templateclass ThreadsafeQueue{private: mutable std::mutex _mut; std::queue> _dataQueue; std::condition_variable _dataCo...
阅读全文
浙公网安备 33010602011771号