Skip to content

Commit

Permalink
add method to limit number of parallel jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
antialize committed Nov 22, 2024
1 parent 0564adc commit 46b5530
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
11 changes: 11 additions & 0 deletions tpie/job.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class job_manager {
/// \brief Initialize the thread pool.
///////////////////////////////////////////////////////////////////////////
void init_pool(size_t threads) {
m_kill_job_pool = false;
m_thread_pool.resize(threads);
for (size_t i = 0; i < threads; ++i) {
std::function<void()> f(worker);
Expand All @@ -69,6 +70,9 @@ class job_manager {
}
}

size_t worker_count() const noexcept {
return m_thread_pool.size();
}
private:

tpie::internal_queue<tpie::job *> m_jobs;
Expand Down Expand Up @@ -125,6 +129,13 @@ void finish_job() {
the_job_manager = 0;
}

void set_worker_count(memory_size_type workers) {
if (the_job_manager->worker_count() != workers) {
the_job_manager->shutdown_pool();
the_job_manager->init_pool(workers);
}
}

job::job()
: m_dependencies(0)
, m_parent(0)
Expand Down
5 changes: 5 additions & 0 deletions tpie/job.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ class TPIE_EXPORT job {
///////////////////////////////////////////////////////////////////////////////
TPIE_EXPORT memory_size_type default_worker_count();

///////////////////////////////////////////////////////////////////////////////
/// \brief Change the number of jobs to run in parallel
///////////////////////////////////////////////////////////////////////////////
TPIE_EXPORT void set_worker_count(memory_size_type);

///////////////////////////////////////////////////////////////////////////////
/// \internal \brief Used by tpie_init to initialize the job subsystem.
///////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 46b5530

Please sign in to comment.