2019-06-19 14:34:30 +02:00
|
|
|
|
/*
|
|
|
|
|
* Copyright © 2004 Ondra Kamenik
|
|
|
|
|
* Copyright © 2019 Dynare Team
|
|
|
|
|
*
|
|
|
|
|
* This file is part of Dynare.
|
|
|
|
|
*
|
|
|
|
|
* Dynare is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* Dynare is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2021-06-09 17:33:48 +02:00
|
|
|
|
* along with Dynare. If not, see <https://www.gnu.org/licenses/>.
|
2019-06-19 14:34:30 +02:00
|
|
|
|
*/
|
2019-01-08 16:09:25 +01:00
|
|
|
|
|
|
|
|
|
// Simple threads.
|
|
|
|
|
|
2019-06-19 17:33:01 +02:00
|
|
|
|
/* This file defines types making a simple interface to multi-threading.
|
2019-01-08 16:09:25 +01:00
|
|
|
|
|
2019-01-28 18:39:42 +01:00
|
|
|
|
The file provides the following interfaces:
|
2019-01-08 16:09:25 +01:00
|
|
|
|
|
2019-06-19 17:33:01 +02:00
|
|
|
|
— detach_thread is a pure virtual class, which must be inherited and a
|
|
|
|
|
method operator()() be implemented as the running code of the thread.
|
|
|
|
|
|
|
|
|
|
— detach_thread_group allows insertion of detach_thread’s and running all of
|
|
|
|
|
them simultaneously. The threads are not joined, they are synchronized by
|
|
|
|
|
means of a counter counting running threads. A change of the counter is
|
|
|
|
|
checked by waiting on an associated condition. The number of maximum
|
|
|
|
|
parallel threads can be controlled. See below. The group also provides a
|
|
|
|
|
mutex to be shared between the workers for their own synchronization
|
|
|
|
|
purposes.
|
|
|
|
|
|
|
|
|
|
The number of maximum parallel threads is controlled via a static member of
|
|
|
|
|
the detach_thread_group class. */
|
2019-01-08 16:09:25 +01:00
|
|
|
|
|
|
|
|
|
#ifndef STHREAD_H
|
|
|
|
|
#define STHREAD_H
|
|
|
|
|
|
2019-01-28 18:39:42 +01:00
|
|
|
|
#include <vector>
|
2019-01-08 16:09:25 +01:00
|
|
|
|
#include <map>
|
2019-01-14 12:19:00 +01:00
|
|
|
|
#include <memory>
|
|
|
|
|
#include <utility>
|
2019-01-28 18:39:42 +01:00
|
|
|
|
#include <thread>
|
|
|
|
|
#include <mutex>
|
|
|
|
|
#include <condition_variable>
|
2019-01-08 16:09:25 +01:00
|
|
|
|
|
|
|
|
|
namespace sthread
|
|
|
|
|
{
|
2019-01-28 18:39:42 +01:00
|
|
|
|
class detach_thread
|
2019-01-08 16:09:25 +01:00
|
|
|
|
{
|
|
|
|
|
public:
|
2019-01-28 18:39:42 +01:00
|
|
|
|
virtual ~detach_thread() = default;
|
2019-01-29 15:52:56 +01:00
|
|
|
|
virtual void operator()(std::mutex &mut) = 0;
|
2019-01-08 16:09:25 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class detach_thread_group
|
|
|
|
|
{
|
2019-01-28 18:39:42 +01:00
|
|
|
|
std::vector<std::unique_ptr<detach_thread>> tlist;
|
2019-01-29 15:52:56 +01:00
|
|
|
|
std::mutex mut_cv; // For the condition variable and the counter
|
2019-01-28 18:39:42 +01:00
|
|
|
|
std::condition_variable cv;
|
|
|
|
|
int counter{0};
|
2019-01-29 15:52:56 +01:00
|
|
|
|
std::mutex mut_threads; // Passed to the workers and shared between them
|
2019-01-08 16:09:25 +01:00
|
|
|
|
public:
|
|
|
|
|
static int max_parallel_threads;
|
|
|
|
|
|
|
|
|
|
void
|
2019-01-28 18:39:42 +01:00
|
|
|
|
insert(std::unique_ptr<detach_thread> c)
|
2019-01-08 16:09:25 +01:00
|
|
|
|
{
|
2019-01-28 18:39:42 +01:00
|
|
|
|
tlist.push_back(std::move(c));
|
2019-01-08 16:09:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 12:19:00 +01:00
|
|
|
|
~detach_thread_group() = default;
|
2019-01-08 16:09:25 +01:00
|
|
|
|
|
2019-01-28 18:39:42 +01:00
|
|
|
|
void run();
|
2019-01-08 16:09:25 +01:00
|
|
|
|
};
|
2019-02-13 18:29:34 +01:00
|
|
|
|
|
|
|
|
|
int default_threads_number();
|
2019-01-08 16:09:25 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif
|