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
|
|
|
|
* along with Dynare. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2019-01-08 16:09:25 +01:00
|
|
|
|
|
|
|
// Simple threads.
|
|
|
|
|
|
|
|
/* This file defines types making a simple interface to
|
2019-01-28 18:39:42 +01:00
|
|
|
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
|
|
|
\unorderedlist
|
2019-01-28 18:39:42 +01:00
|
|
|
\li |detach_thread| is a pure virtual class, which must be inherited and a
|
2019-01-08 16:09:25 +01:00
|
|
|
method |operator()()| be implemented as the running code of the
|
2019-01-28 18:39:42 +01:00
|
|
|
thread.
|
|
|
|
\li |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
|
2019-01-29 15:52:56 +01:00
|
|
|
threads can be controlled. See below. The group also provides a mutex to be
|
|
|
|
shared between the workers for their own synchronization purposes.
|
2019-01-08 16:09:25 +01:00
|
|
|
\endunorderedlist
|
|
|
|
|
|
|
|
The number of maximum parallel threads is controlled via a static
|
2019-01-28 18:39:42 +01:00
|
|
|
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
|
|
|
|
{
|
|
|
|
/* The detached thread is the same as joinable |thread|. We only
|
|
|
|
re-implement |run| method to call |thread_traits::detach_run|, and add
|
|
|
|
a method which installs a counter. The counter is increased and
|
|
|
|
decreased on the body of the new thread. */
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
/* The detach thread group is (by interface) the same as
|
|
|
|
|thread_group|. The extra thing we have here is the |counter|. The
|
|
|
|
implementation of |insert| and |run| is different. */
|
|
|
|
|
|
|
|
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
|