Modules/Threading/public/ParallelFor.hpp
agent b552d7c324 Port agent-work onto master
Brings the agent-work branch tip onto master, keeping master's architecture
and putting every agent-work feature on top of it: the window/GpuDevice/
RenderTarget split, explicit per-instance profilers instead of a global, the
input layer with scroll sources and ends, the Widgets core and components
(shared SelectionModel/Commands lists, popup stack, titled panels, prompt
dialogs, file browser, kanban, outline tree, markdown, docking), the gallery
on the new UI, the Objects explorer and interpreter fixes, the Taskboard, the
Sketch2D render path and its tests and bench, and the headless Wayland
runner the UI tests drive.

No MODULES_BUILD_* switches are introduced: platform differences are plain
if (ANDROID) / if (NOT ANDROID).
2026-11-02 11:00:51 +03:00

44 lines
1,010 B
C++

#pragma once
#include <atomic>
#include <condition_variable>
#include <cstddef>
#include <functional>
#include <mutex>
#include <thread>
#include <vector>
namespace tp {
class ParallelFor {
public:
using ThreadStart = std::function<void(size_t index)>;
explicit ParallelFor(size_t threads, ThreadStart onStart = {});
~ParallelFor();
ParallelFor(const ParallelFor&) = delete;
ParallelFor& operator=(const ParallelFor&) = delete;
[[nodiscard]] size_t threads() const { return mThreads.size() + 1; }
void run(size_t count, const std::function<void(size_t index)>& body);
private:
void work(size_t generation);
void drain();
std::vector<std::thread> mThreads;
ThreadStart mOnStart;
std::mutex mMutex;
std::condition_variable mStart;
std::condition_variable mDone;
const std::function<void(size_t)>* mBody = nullptr;
size_t mCount = 0;
std::atomic<size_t> mNext{ 0 };
size_t mBusy = 0;
size_t mGeneration = 0;
size_t mStarted = 0;
bool mStopping = false;
};
}