Browse Source

HPCC-24185 Support asyncFor(...) that takes a lambda function

Signed-off-by: Gavin Halliday <gavin.halliday@lexisnexis.com>
Gavin Halliday 5 years ago
parent
commit
4d6e415d29
1 changed files with 34 additions and 0 deletions
  1. 34 0
      system/jlib/jthread.hpp

+ 34 - 0
system/jlib/jthread.hpp

@@ -206,6 +206,40 @@ public:
     virtual void Do(unsigned idx=0)=0;
 };
 
+template <typename AsyncFunc>
+class CAsyncForFunc final : public CAsyncFor
+{
+public:
+    CAsyncForFunc(AsyncFunc _func) : func(_func) {}
+    virtual void Do(unsigned idx=0) { func(idx); }
+private:
+    AsyncFunc func;
+};
+
+//Utility functions for executing a lambda function in parallel, but allow the number of concurrent iterations
+//action on exception, and shuffling to be controlled.
+template <typename AsyncFunc>
+inline void asyncFor(unsigned num, unsigned maxAtOnce, bool abortFollowingException, bool shuffled, AsyncFunc func)
+{
+    CAsyncForFunc<AsyncFunc> async(func);
+    async.For(num, maxAtOnce, abortFollowingException, shuffled);
+}
+template <typename AsyncFunc>
+inline void asyncFor(unsigned num, unsigned maxAtOnce, bool abortFollowingException, AsyncFunc func)
+{
+    asyncFor(num, maxAtOnce, abortFollowingException, false, func);
+}
+template <typename AsyncFunc>
+inline void asyncFor(unsigned num, unsigned maxAtOnce, AsyncFunc func)
+{
+    asyncFor(num, maxAtOnce, false, false, func);
+}
+template <typename AsyncFunc>
+inline void asyncFor(unsigned num, AsyncFunc func)
+{
+    asyncFor(num, num, false, false, func);
+}
+
 // ---------------------------------------------------------------------------
 // Thread Pools
 // ---------------------------------------------------------------------------