Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

  • 2319abe
  • /
  • src
  • /
  • thread_pool.cpp
Raw File Download

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
content badge
swh:1:cnt:146d372d59d357780fbe916e2935bc5fcd161dbb
directory badge
swh:1:dir:a17483be91dca897dc501ad58bd42dda28c81df8

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • content
  • directory
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
thread_pool.cpp
/*
 * Copyright (c) 2022, NVIDIA CORPORATION.  All rights reserved.
 *
 * NVIDIA CORPORATION and its licensors retain all intellectual property
 * and proprietary rights in and to this software, related documentation
 * and any modifications thereto.  Any use, reproduction, disclosure or
 * distribution of this software and related documentation without an express
 * license agreement from NVIDIA CORPORATION is strictly prohibited.
 */

// This file was taken from the tev image viewer and is re-released here
// under the NVIDIA Source Code License with permission from the author.

#include <neural-graphics-primitives/common.h>
#include <neural-graphics-primitives/thread_pool.h>

#include <chrono>

namespace ngp {

ThreadPool::ThreadPool()
: ThreadPool{std::thread::hardware_concurrency()} {}

ThreadPool::ThreadPool(size_t max_num_threads, bool force) {
	if (!force) {
		max_num_threads = std::min((size_t)std::thread::hardware_concurrency(), max_num_threads);
	}
	start_threads(max_num_threads);
}

ThreadPool::~ThreadPool() {
	wait_until_queue_completed();
	shutdown_threads(m_threads.size());
}

void ThreadPool::start_threads(size_t num) {
	m_num_threads += num;
	for (size_t i = m_threads.size(); i < m_num_threads; ++i) {
		m_threads.emplace_back([this, i] {
			while (true) {
				std::unique_lock<std::mutex> lock{m_task_queue_mutex};

				// look for a work item
				while (i < m_num_threads && m_task_queue.empty()) {
					// if there are none, signal that the queue is completed
					// and wait for notification of new work items.
					m_task_queue_completed_condition.notify_all();
					m_worker_condition.wait(lock);
				}

				if (i >= m_num_threads) {
					break;
				}

				std::function<void()> task{move(m_task_queue.front())};
				m_task_queue.pop_front();

				// Unlock the lock, so we can process the task without blocking other threads
				lock.unlock();

				task();
			}
		});
	}
}

void ThreadPool::shutdown_threads(size_t num) {
	auto num_to_close = std::min(num, m_num_threads);

	{
		std::lock_guard<std::mutex> lock{m_task_queue_mutex};
		m_num_threads -= num_to_close;
	}

	// Wake up all the threads to have them quit
	m_worker_condition.notify_all();
	for (auto i = 0u; i < num_to_close; ++i) {
		m_threads.back().join();
		m_threads.pop_back();
	}
}

void ThreadPool::set_n_threads(size_t num) {
	if (m_num_threads > num) {
		shutdown_threads(m_num_threads - num);
	} else if (m_num_threads < num) {
		start_threads(num - m_num_threads);
	}
}

void ThreadPool::wait_until_queue_completed() {
	std::unique_lock<std::mutex> lock{m_task_queue_mutex};
	m_task_queue_completed_condition.wait(lock, [this]() { return m_task_queue.empty(); });
}

void ThreadPool::flush_queue() {
	std::lock_guard<std::mutex> lock{m_task_queue_mutex};
	m_task_queue.clear();
}

}

back to top

Software Heritage — Copyright (C) 2015–2025, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API