libosmscout  1.1.1
Worker.h
Go to the documentation of this file.
1 #ifndef OSMSCOUT_UTIL_WORKER_H
2 #define OSMSCOUT_UTIL_WORKER_H
3 
4 /*
5  This source is part of the libosmscout library
6  Copyright (C) 2020 Tim Teulings
7 
8  This library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Lesser General Public
10  License as published by the Free Software Foundation; either
11  version 2.1 of the License, or (at your option) any later version.
12 
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public
19  License along with this library; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22 
23 #include <thread>
24 #include <optional>
25 
27 
29 
30 namespace osmscout {
31 
44  {
45  private:
46  std::thread thread;
47  bool success=true;
48 
49  protected:
63  success=false;
64  }
65 
66  public:
67  ThreadedWorker() = default;
68 
69  ThreadedWorker(const ThreadedWorker& other) = delete;
70  ThreadedWorker(ThreadedWorker&& other) = delete;
71 
72  virtual ~ThreadedWorker() = default;
73 
83  bool WasSuccessful() const
84  {
85  return success;
86  }
87 
88  void Wait() {
89  if (thread.joinable()) {
90  thread.join();
91  }
92  }
93 
94  protected:
95  void Start()
96  {
97  thread = std::thread(&ThreadedWorker::ProcessingLoop,this);
98  }
99 
100  virtual void ProcessingLoop() = 0;
101  };
102 
112  template <typename E>
113  class Producer : public ThreadedWorker
114  {
115  protected:
117 
118  public:
120  : outQueue(outQueue)
121  {
122  }
123  };
124 
134  template <typename E1, typename E2>
135  class Pipe : public ThreadedWorker
136  {
137  protected:
140 
141  public:
144  : inQueue(inQueue),
146  {
147  }
148  };
149 
158  template <typename E>
159  class Consumer : public ThreadedWorker
160  {
161  protected:
163 
164  public:
166  : inQueue(inQueue)
167  {
168  }
169  };
170 
171 }
172 
173 #endif
Definition: Worker.h:43
void Start()
Definition: Worker.h:95
void Wait()
Definition: Worker.h:88
ProcessingQueue< E > & outQueue
Definition: Worker.h:116
ProcessingQueue< E2 > & outQueue
Definition: Worker.h:139
bool WasSuccessful() const
Definition: Worker.h:83
Definition: Area.h:38
virtual void ProcessingLoop()=0
ProcessingQueue< E1 > & inQueue
Definition: Worker.h:138
#define OSMSCOUT_API
Definition: CoreImportExport.h:45
ProcessingQueue< E > & inQueue
Definition: Worker.h:162
Pipe(ProcessingQueue< E1 > &inQueue, ProcessingQueue< E2 > &outQueue)
Definition: Worker.h:142
Definition: Worker.h:135
Consumer(ProcessingQueue< E > &inQueue)
Definition: Worker.h:165
Definition: Worker.h:159
Definition: Worker.h:113
Producer(ProcessingQueue< E > &outQueue)
Definition: Worker.h:119
void MarkWorkerAsFailed()
Definition: Worker.h:62