Advertisement
Guest User

Untitled

a guest
May 14th, 2014
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <thread>
  4. #include <atomic>
  5. #include <Diverse\LowLevel.h>
  6.  
  7. class Barrier
  8. {
  9. public:
  10.     typedef uint64_t Tracer;
  11.  
  12.     Barrier(const uint8_t NewThreadsTotal) :
  13.         ThreadsTotal(NewThreadsTotal),
  14.         GlobalProgress(0),
  15.         ThreadCount(0)
  16.     {
  17.     }
  18.  
  19.     __inline uint8_t Set(Tracer& TracerObj)
  20.     {
  21.         const uint8_t CurrentThreadId = ThreadCount.fetch_add(1);
  22.         if (CurrentThreadId == ThreadsTotal - 1)
  23.         {
  24.             ThreadCount = 0;
  25.             ++GlobalProgress;
  26.         }
  27.         Wait(TracerObj);
  28.         return CurrentThreadId;
  29.     }
  30.  
  31.     __inline Tracer GetTracer()
  32.     {
  33.         return GlobalProgress;
  34.     }
  35.  
  36. private:
  37.     __inline void Wait(Tracer& TracerObj)
  38.     {
  39.         while (TracerObj >= GlobalProgress)
  40.             std::this_thread::yield();
  41.         ++TracerObj;
  42.     }
  43.     std::atomic<uint8_t>    ThreadCount;
  44.     uint64_t                GlobalProgress;
  45.     const uint8_t           ThreadsTotal;
  46. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement