Turi Create  4.0
TaskQueue.hpp
1 /* Copyright © 2020 Apple Inc. All rights reserved.
2  *
3  * Use of this source code is governed by a BSD-3-clause license that can
4  * be found in the LICENSE.txt file or at
5  * https://opensource.org/licenses/BSD-3-Clause
6  */
7 
8 #pragma once
9 
10 #include <functional>
11 #include <memory>
12 
13 namespace turi {
14 namespace neural_net {
15 
16 /// Abstract task queue interface modeled after Grand Central Dispatch
17 class TaskQueue {
18  public:
19  /// Returns a task queue that does not enforce any ordering among its tasks
20  /// and that shares system resources with other task queues created by this
21  /// function.
22  static std::shared_ptr<TaskQueue> GetGlobalConcurrentQueue();
23 
24  /// Returns a task queue that guarantees that if task A is submitted before
25  /// task B, then task A will finish before task B begins. Accepts a label that
26  /// may be used by the system to identify work done by this queue.
27  static std::unique_ptr<TaskQueue> CreateSerialQueue(const char* label);
28 
29  virtual ~TaskQueue() = default;
30 
31  /// Submits a function to this task queue without waiting for the function to
32  /// finish. The task must not throw an exception.
33  virtual void DispatchAsync(std::function<void()> task) = 0;
34 
35  /// Submits a function to this task queue and waits for the function to
36  /// execute. The task must not throw an exception.
37  virtual void DispatchSync(std::function<void()> task) = 0;
38 
39  /// Submits a function to this task queue n times, with arguments ranging from
40  /// 0 to n - 1. When dispatched to a concurrent queue, the function must be
41  /// reentrant. Rethrows the first exception thrown by any task invocation.
42  virtual void DispatchApply(size_t n, std::function<void(size_t i)> task) = 0;
43 };
44 
45 } // namespace neural_net
46 } // namespace turi
virtual void DispatchSync(std::function< void()> task)=0
static std::shared_ptr< TaskQueue > GetGlobalConcurrentQueue()
static std::unique_ptr< TaskQueue > CreateSerialQueue(const char *label)
virtual void DispatchAsync(std::function< void()> task)=0
Abstract task queue interface modeled after Grand Central Dispatch.
Definition: TaskQueue.hpp:17
virtual void DispatchApply(size_t n, std::function< void(size_t i)> task)=0