Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <thread>
- #include <atomic>
- #define THREADPOOL_THREADMAX (16)
- class ThreadPool
- {
- public:
- typedef uint64_t Progress;
- template<typename T>
- void Start(T Function, const size_t NewThreadNum)
- {
- if (ThreadNum > 0) return;//läuft schon
- ThreadNum = NewThreadNum;
- for (int i = 0; i < ThreadNum; ++i)
- Thread[i] = std::thread(Function);
- }
- void Join()
- {
- for (int i = 0; i < ThreadNum; ++i)
- Thread[i].join();
- ThreadNum = 0;
- }
- void Sync(Progress& LocalProgress)
- {
- ++LocalProgress;
- if ((FinishCounter.fetch_add(1) + 1) == ThreadNum)
- {
- FinishCounter = 0;
- ++GlobalProgress;
- }
- while (LocalProgress > GlobalProgress)
- std::this_thread::sleep_for(std::chrono::milliseconds(0));
- }
- private:
- size_t ThreadNum = 0;
- std::thread Thread[THREADPOOL_THREADMAX];
- std::atomic<size_t> FinishCounter = 0;
- Progress GlobalProgress = 0;
- };
Advertisement
Add Comment
Please, Sign In to add comment