client-cpp  0.9.0
AbstractExecutorContext.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2014-2016 CyberVision, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ABSTRACTEXECUTORCONTEXT_HPP_
18 #define ABSTRACTEXECUTORCONTEXT_HPP_
19 
20 #include <memory>
21 
22 #include "kaa/KaaThread.hpp"
23 #include "kaa/utils/ThreadPool.hpp"
25 
26 namespace kaa {
27 
29 public:
31  : useCount_(0), awaitTerminationTimeout_(5)
32  {}
33 
34  virtual void init();
35  virtual void stop();
36 
37  void setAwaitTerminationTimeout(std::size_t awaitTerminationTimeout)
38  {
39  awaitTerminationTimeout_ = awaitTerminationTimeout;
40  }
41 
43  {
44  return awaitTerminationTimeout_;
45  }
46 
47 protected:
48  IThreadPoolPtr createExecutor(std::size_t threadCount)
49  {
50  return std::make_shared<ThreadPool>(threadCount);
51  }
52 
54  {
55  if (threadPool) {
56  threadPool->awaitTermination(awaitTerminationTimeout_);
57  }
58  }
59 
60 protected:
61  virtual void doInit() = 0;
62  virtual void doStop() = 0;
63 
64 private:
65  std::size_t useCount_;
66  KAA_MUTEX_DECLARE(useCountGuard_);
67 
68  std::size_t awaitTerminationTimeout_; // in seconds
69 };
70 
71 } /* namespace kaa */
72 
73 #endif /* ABSTRACTEXECUTORCONTEXT_HPP_ */
void setAwaitTerminationTimeout(std::size_t awaitTerminationTimeout)
void shutdownExecutor(IThreadPoolPtr threadPool)
std::shared_ptr< IThreadPool > IThreadPoolPtr
Definition: IThreadPool.hpp:40
IThreadPoolPtr createExecutor(std::size_t threadCount)