summaryrefslogtreecommitdiffstats
path: root/ml/dlib/dlib/threads/async_abstract.h
blob: a9fa1e45883499073e92fdb690ab781230beb949 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright (C) 2016  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.
#undef DLIB_AsYNC_ABSTRACT_Hh_
#ifdef DLIB_AsYNC_ABSTRACT_Hh_ 

#include "thread_pool_extension_abstract.h"
#include <future>
#include <functional>

namespace dlib
{

// ----------------------------------------------------------------------------------------

    thread_pool& default_thread_pool(
    );
    /*!
        ensures
            - returns a reference to a global thread_pool.  If the DLIB_NUM_THREADS
              environment variable is set to an integer then the thread pool will contain
              DLIB_NUM_THREADS threads, otherwise it will contain
              std::thread::hardware_concurrency() threads.
    !*/

// ----------------------------------------------------------------------------------------

    template < 
        typename Function, 
        typename ...Args
        >
    std::future<typename std::result_of<Function(Args...)>::type> async(
        thread_pool& tp, 
        Function&& f, 
        Args&&... args 
    );
    /*!
        requires
            - f must be a function and f(args...) must be a valid expression.
        ensures
            - This function behaves just like std::async(std::launch::async, f, args)
              except that instead of spawning a new thread to process each task it submits
              the task to the provided dlib::thread_pool.  Therefore, dlib::async() is
              guaranteed to use a bounded number of threads unlike std::async().  This also
              means that calls to dlib::async() will block if there aren't any free threads
              in the thread pool.
    !*/

// ----------------------------------------------------------------------------------------

    template < 
        typename Function, 
        typename ...Args
        >
    std::future<typename std::result_of<Function(Args...)>::type> async(
        Function&& f, 
        Args&&... args 
    );
    /*!
        ensures
            - Calling this function is equivalent to directly calling async(default_thread_pool(), f, args...)
    !*/
}

// ----------------------------------------------------------------------------------------

#endif // DLIB_AsYNC_ABSTRACT_Hh_