Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <thread>
- #include <atomic>
- #include <Diverse\LowLevel.h>
- class Barrier
- {
- public:
- typedef uint64_t Tracer;
- Barrier(const uint8_t NewThreadsTotal) :
- ThreadsTotal(NewThreadsTotal),
- GlobalProgress(0),
- ThreadCount(0)
- {
- }
- __inline uint8_t Set(Tracer& TracerObj)
- {
- const uint8_t CurrentThreadId = ThreadCount.fetch_add(1);
- if (CurrentThreadId == ThreadsTotal - 1)
- {
- ThreadCount = 0;
- ++GlobalProgress;
- }
- Wait(TracerObj);
- return CurrentThreadId;
- }
- __inline Tracer GetTracer()
- {
- return GlobalProgress;
- }
- private:
- __inline void Wait(Tracer& TracerObj)
- {
- while (TracerObj >= GlobalProgress)
- std::this_thread::yield();
- ++TracerObj;
- }
- std::atomic<uint8_t> ThreadCount;
- uint64_t GlobalProgress;
- const uint8_t ThreadsTotal;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement