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