Scroom  0.14
threadpoolimpl.hh
Go to the documentation of this file.
1 /*
2  * Scroom - Generic viewer for 2D data
3  * Copyright (C) 2009-2022 Kees-Jan Dijkzeul
4  *
5  * SPDX-License-Identifier: LGPL-2.1
6  */
7 
8 #pragma once
9 
10 #include <scroom/threadpool.hh>
11 
13 // Code...
14 
15 namespace Detail
16 {
17  template <typename R, typename T>
18  R threadPoolExecute(boost::shared_ptr<T> fn)
19  {
20  return (*fn)();
21  }
22 } // namespace Detail
23 
24 template <typename T>
25 void ThreadPool::schedule(boost::shared_ptr<T> fn, int priority, const ThreadPool::Queue::Ptr& queue)
26 {
27  schedule(fn, priority, queue->getWeak());
28 }
29 
30 template <typename T>
31 void ThreadPool::schedule(boost::shared_ptr<T> fn, const ThreadPool::Queue::Ptr& queue)
32 {
33  schedule(fn, defaultPriority, queue);
34 }
35 
36 template <typename T>
37 void ThreadPool::schedule(boost::shared_ptr<T> fn, int priority, ThreadPool::WeakQueue::Ptr queue)
38 {
39  schedule(boost::bind(Detail::threadPoolExecute<void, T>, fn), priority, queue);
40 }
41 
42 template <typename T>
43 void ThreadPool::schedule(boost::shared_ptr<T> fn, ThreadPool::WeakQueue::Ptr queue)
44 {
45  schedule(fn, defaultPriority, queue);
46 }
47 
48 template <typename R>
49 boost::unique_future<R> ThreadPool::schedule(boost::function<R()> const& fn, int priority, const ThreadPool::Queue::Ptr& queue)
50 {
51  return schedule(fn, priority, queue->getWeak());
52 }
53 
54 template <typename R>
55 boost::unique_future<R> ThreadPool::schedule(boost::function<R()> const& fn, const ThreadPool::Queue::Ptr& queue)
56 {
57  return schedule(fn, defaultPriority, queue);
58 }
59 
60 template <typename R, typename T>
61 boost::unique_future<R> ThreadPool::schedule(boost::shared_ptr<T> fn, int priority, const ThreadPool::Queue::Ptr& queue)
62 {
63  return schedule<R, T>(fn, priority, queue->getWeak());
64 }
65 
66 template <typename R, typename T>
67 boost::unique_future<R> ThreadPool::schedule(boost::shared_ptr<T> fn, const ThreadPool::Queue::Ptr& queue)
68 {
69  return schedule<R, T>(fn, defaultPriority, queue);
70 }
71 
72 template <typename R>
73 boost::unique_future<R> ThreadPool::schedule(boost::function<R()> const& fn, int priority, ThreadPool::WeakQueue::Ptr queue)
74 {
75  // Todo: If boost::function supported move semantics, we could do without
76  // the shared pointer.
77 
78  // Todo: Without the static cast, Boost 1.53 packaged_task stores a
79  // reference to fn, which is a temporary, and hence results in
80  // undefined behaviour. Move semantics seem to work OK there...
81  //
82  // See https://svn.boost.org/trac/boost/ticket/8596
83  boost::shared_ptr<boost::packaged_task<R>> const t(new boost::packaged_task<R>(static_cast<boost::function<R()>>(fn)));
84  boost::unique_future<R> f = t->get_future();
85  schedule(boost::bind(Detail::threadPoolExecute<void, boost::packaged_task<R>>, t), priority, queue);
86  return f;
87 }
88 
89 template <typename R>
90 boost::unique_future<R> ThreadPool::schedule(boost::function<R()> const& fn, ThreadPool::WeakQueue::Ptr queue)
91 {
92  return schedule(fn, defaultPriority, queue);
93 }
94 
95 template <typename R, typename T>
96 boost::unique_future<R> ThreadPool::schedule(boost::shared_ptr<T> fn, int priority, ThreadPool::WeakQueue::Ptr queue)
97 {
98  // Todo: If boost::function supported move semantics, we could do without
99  // the shared pointer.
100  boost::shared_ptr<boost::packaged_task<R>> const t(
101  new boost::packaged_task<R>(boost::bind(Detail::threadPoolExecute<R, T>, fn)));
102  boost::unique_future<R> f = t->get_future();
103  schedule(boost::bind(Detail::threadPoolExecute<void, boost::packaged_task<R>>, t), priority, queue);
104  return f;
105 }
106 
107 template <typename R, typename T>
108 boost::unique_future<R> ThreadPool::schedule(boost::shared_ptr<T> fn, ThreadPool::WeakQueue::Ptr queue)
109 {
110  return schedule(fn, defaultPriority, queue);
111 }
ThreadPool::Queue::Ptr
boost::shared_ptr< Queue > Ptr
Definition: threadpool.hh:82
threadpool.hh
ThreadPool::WeakQueue::Ptr
boost::shared_ptr< WeakQueue > Ptr
Definition: threadpool.hh:137
Detail::threadPoolExecute
R threadPoolExecute(boost::shared_ptr< T > fn)
Definition: threadpoolimpl.hh:18
ThreadPool::defaultPriority
static const int defaultPriority
Definition: threadpool.hh:270
Detail
Definition: async-deleter.hh:12
ThreadPool::schedule
void schedule(boost::function< void()> const &fn, int priority=defaultPriority, const Queue::Ptr &queue=defaultQueue())
Definition: threadpoolimpl.cc:342