NASA Astrobee Robot Software  0.19.1
Flight software for the Astrobee robots operating inside the International Space Station.
thread.h
Go to the documentation of this file.
1 /* Copyright (c) 2017, United States Government, as represented by the
2  * Administrator of the National Aeronautics and Space Administration.
3  *
4  * All rights reserved.
5  *
6  * The Astrobee platform is licensed under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations
16  * under the License.
17  */
18 
19 #ifndef FF_COMMON_THREAD_H_
20 #define FF_COMMON_THREAD_H_
21 
22 #include <gflags/gflags.h>
23 #include <pthread.h>
24 
25 #include <list>
26 #include <functional>
27 
28 DECLARE_int32(num_threads);
29 
30 #define GOOGLE_ALLOW_RVALUE_REFERENCES_PUSH
31 #define GOOGLE_ALLOW_RVALUE_REFERENCES_POP
32 
34 
35 namespace ff_common {
36 
37  void* HolderFunction(void* ptr);
38 
39  class ThreadPool {
40  public:
41  typedef std::tuple<std::function<void(void)>, pthread_mutex_t*, pthread_cond_t*, bool> VarsTuple;
42 
43  ThreadPool();
44  ~ThreadPool();
45  // The following identifies this thread as non copyable and non
46  // moveable. Our threads are holding pointers to this exact
47  // object.
48  ThreadPool(const ThreadPool&) = delete;
49  ThreadPool& operator=(const ThreadPool&) = delete;
50 
51  // This pushes back a function and it's arguments to be
52  // executed. This method will block if the number of threads is
53  // already maxed. You can also push back mixed types of functions.
54  //
55  // Example:
56  // void Monkey(std::vector const& input, int val, std::vector * output);
57  // ThreadPool pool;
58  // pool.AddTask(Monkey, std::ref(input), 3, &output);
59  //
60  // If you don't use a std::ref as in the above example, the input
61  // will be copied. Other alternatives are to use a pointer.
62  template <typename Function, typename... Args>
63  void AddTask(Function&& f, Args&&... args) {
64  WaitTillJobOpening();
65 
66  // Bind up the function the user has given us
67  var_bindings_.emplace_back(std::bind(f, args...),
68  &cond_mutex_,
69  &cond_, false);
70 
71  // Create the thread and start doing work
72  threads_.push_back(pthread_t());
73  pthread_create(&threads_.back(), NULL, HolderFunction, &var_bindings_.back());
74  }
75 
76  void Join();
77 
78  private:
79  void WaitTillJobOpening();
80 
81  size_t max_concurrent_jobs_;
82  std::list<pthread_t> threads_;
83  std::list<VarsTuple> var_bindings_;
84  pthread_mutex_t cond_mutex_;
85  pthread_cond_t cond_;
86  };
87 
88 } // namespace ff_common
89 
91 
92 #endif // FF_COMMON_THREAD_H_
ff_common::HolderFunction
void * HolderFunction(void *ptr)
Definition: thread.cc:30
ff_common::ThreadPool
Definition: thread.h:39
ff_common::ThreadPool::Join
void Join()
Definition: thread.cc:62
ff_common::ThreadPool::AddTask
void AddTask(Function &&f, Args &&... args)
Definition: thread.h:63
ff_common::ThreadPool::operator=
ThreadPool & operator=(const ThreadPool &)=delete
ff_common
Definition: init.h:29
ff_common::ThreadPool::~ThreadPool
~ThreadPool()
Definition: thread.cc:56
GOOGLE_ALLOW_RVALUE_REFERENCES_PUSH
#define GOOGLE_ALLOW_RVALUE_REFERENCES_PUSH
Definition: thread.h:30
GOOGLE_ALLOW_RVALUE_REFERENCES_POP
#define GOOGLE_ALLOW_RVALUE_REFERENCES_POP
Definition: thread.h:31
DECLARE_int32
DECLARE_int32(num_threads)
ff_common::ThreadPool::VarsTuple
std::tuple< std::function< void(void)>, pthread_mutex_t *, pthread_cond_t *, bool > VarsTuple
Definition: thread.h:41
ff_common::ThreadPool::ThreadPool
ThreadPool()
Definition: thread.cc:47