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
|
|
|
|
|
2019-02-13 18:29:34 +01:00
|
|
|
|
#include <algorithm>
|
2019-01-08 16:09:25 +01:00
|
|
|
|
|
|
|
|
|
#include "sthread.hh"
|
|
|
|
|
|
|
|
|
|
namespace sthread
|
|
|
|
|
{
|
2019-06-19 17:33:01 +02:00
|
|
|
|
/* We set the default value for ‘max_parallel_threads’ to half the number of
|
2019-05-02 12:33:00 +02:00
|
|
|
|
logical CPUs */
|
2019-02-13 18:29:34 +01:00
|
|
|
|
int
|
|
|
|
|
default_threads_number()
|
|
|
|
|
{
|
|
|
|
|
return std::max(1, static_cast<int>(std::thread::hardware_concurrency()) / 2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int detach_thread_group::max_parallel_threads = default_threads_number();
|
2019-01-28 18:39:42 +01:00
|
|
|
|
|
2019-06-19 17:33:01 +02:00
|
|
|
|
/* We cycle through all threads in the group, and in each cycle we wait for
|
|
|
|
|
the change in the ‘counter’. If the counter indicates less than maximum
|
|
|
|
|
parallel threads running, then a new thread is run, and the iterator in
|
|
|
|
|
the list is moved.
|
2019-01-08 16:09:25 +01:00
|
|
|
|
|
2019-01-28 18:39:42 +01:00
|
|
|
|
At the end we have to wait for all thread to finish. */
|
2019-01-08 16:09:25 +01:00
|
|
|
|
void
|
2019-01-28 18:39:42 +01:00
|
|
|
|
detach_thread_group::run()
|
2019-01-08 16:09:25 +01:00
|
|
|
|
{
|
2019-01-29 15:52:56 +01:00
|
|
|
|
std::unique_lock<std::mutex> lk{mut_cv};
|
2019-01-28 18:39:42 +01:00
|
|
|
|
auto it = tlist.begin();
|
|
|
|
|
while (it != tlist.end())
|
|
|
|
|
{
|
|
|
|
|
counter++;
|
|
|
|
|
std::thread th{[&, it] {
|
2019-12-20 14:36:20 +01:00
|
|
|
|
// The ‘it’ variable is captured by value, because otherwise the iterator may move
|
|
|
|
|
(*it)->operator()(mut_threads);
|
|
|
|
|
std::unique_lock<std::mutex> lk2{mut_cv};
|
|
|
|
|
counter--;
|
|
|
|
|
/* First notify the thread waiting on the condition variable, then
|
|
|
|
|
unlock the mutex. We must do these two operations in that order,
|
|
|
|
|
otherwise there is a possibility of having the main process
|
|
|
|
|
destroying the condition variable before the thread tries to
|
|
|
|
|
notify it (if all other threads terminate at the same time and
|
|
|
|
|
bring the counter down to zero).
|
|
|
|
|
For that reason, we cannot use std::notify_all_at_thread_exit() */
|
|
|
|
|
cv.notify_one();
|
|
|
|
|
lk2.unlock();
|
|
|
|
|
}};
|
2019-01-28 18:39:42 +01:00
|
|
|
|
th.detach();
|
|
|
|
|
++it;
|
|
|
|
|
cv.wait(lk, [&] { return counter < max_parallel_threads; });
|
|
|
|
|
}
|
|
|
|
|
cv.wait(lk, [&] { return counter == 0; });
|
2019-01-08 16:09:25 +01:00
|
|
|
|
}
|
|
|
|
|
}
|