threadpool.h
Go to the documentation of this file.
1/* Copyright (C) 2019 Marc Stevens.
2
3 This file is part of fplll. fplll is free software: you
4 can redistribute it and/or modify it under the terms of the GNU Lesser
5 General Public License as published by the Free Software Foundation,
6 either version 2.1 of the License, or (at your option) any later version.
7
8 fplll is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU Lesser General Public License for more details.
12
13 You should have received a copy of the GNU Lesser General Public License
14 along with fplll. If not, see <http://www.gnu.org/licenses/>. */
15
16#ifndef FPLLL_THREADPOOL_H
17#define FPLLL_THREADPOOL_H
18
19#include <fplll/defs.h>
21
23
24/* import C++11 standard mutex, lock_guard<mutex>, unique_lock<mutex> for convenience */
26typedef std::lock_guard<std::mutex> lock_guard;
27typedef std::unique_lock<std::mutex> unique_lock;
28
29/* import thread_pool::barrier
30 class barrier {
31 public:
32 barrier(size_t N);
33 void wait(); // wait until N threads have called wait()
34 }
35*/
37
38/* fplll's threadpool
39
40 Note that for N threads, the total threadpool consists of the main thread and N-1 pooled
41 threads. Default use is to submit N jobs and call wait_work in the main thread, which will cause
42 the main thread to also start processing jobs.
43
44 class threadpool {
45 public:
46
47 // simple way to add a job
48 void push(const std::function<void()>& f);
49 void push(std::function<void()>&& f);
50
51 // advanced way to add job
52 template<typename F, typename... Args>
53 auto enqueue(F&& f, Args&&... args) -> std::future<typename
54 std::result_of<F(Args...)>::type>;
55
56 // process tasks with main thread & then wait until all threads are idle
57 // DO NOT CALL FROM A JOB FUNCTION: WILL CAUSE DEADLOCK
58 void wait_work();
59
60 }
61*/
62
64
65/* get and set number of threads in threadpool, both return the (new) number of threads */
66int get_threads();
67int set_threads(int th = -1); // -1 defaults number of threads to machine's number of cores
68
70
71#endif
Definition: thread_pool.hpp:150
Definition: thread_pool.hpp:101
#define FPLLL_END_NAMESPACE
Definition: defs.h:117
#define FPLLL_BEGIN_NAMESPACE
Definition: defs.h:114
std::lock_guard< std::mutex > lock_guard
Definition: threadpool.h:26
int set_threads(int th=-1)
Definition: threadpool.cpp:25
thread_pool::thread_pool threadpool
Definition: threadpool.cpp:20
thread_pool::barrier barrier
Definition: threadpool.h:36
FPLLL_BEGIN_NAMESPACE typedef std::mutex mutex
Definition: threadpool.h:25
std::unique_lock< std::mutex > unique_lock
Definition: threadpool.h:27
int get_threads()
Definition: threadpool.cpp:23