client-cpp  0.10.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  virtual void doInit() = 0;
49  virtual void doStop() = 0;
50 
51 protected:
52  IThreadPoolPtr createExecutor(std::size_t threadCount)
53  {
54  return std::make_shared<ThreadPool>(threadCount);
55  }
56 
58  {
59  if (threadPool) {
60  threadPool->shutdown();
61  threadPool->awaitTermination(awaitTerminationTimeout_);
62  }
63  }
64 
65 private:
66  std::size_t useCount_;
67  KAA_MUTEX_DECLARE(useCountGuard_);
68 
69  std::size_t awaitTerminationTimeout_; // in seconds
70 };
71 
72 } /* namespace kaa */
73 
74 #endif /* ABSTRACTEXECUTORCONTEXT_HPP_ */
void setAwaitTerminationTimeout(std::size_t awaitTerminationTimeout)
void shutdownExecutor(IThreadPoolPtr threadPool)
std::shared_ptr< IThreadPool > IThreadPoolPtr
Definition: IThreadPool.hpp:68
IThreadPoolPtr createExecutor(std::size_t threadCount)