Make methods noexcept where necessary

This commit is contained in:
King_DuckZ 2020-08-09 17:28:31 +01:00
parent f6cc2ff951
commit 399f60190f
2 changed files with 32 additions and 12 deletions

View file

@ -4,6 +4,7 @@
*/
#pragma once
#include <system_error>
#ifndef THREADPOOL_HPP
#define THREADPOOL_HPP
@ -19,6 +20,7 @@
#include <type_traits>
#include <utility>
#include <vector>
#include <iostream>
namespace roar11
{
@ -175,6 +177,22 @@ namespace roar11
return result;
}
void join() noexcept {
for(auto& thread : m_threads)
{
if(thread.joinable())
{
try {
thread.join();
}
catch (const std::system_error& err) {
//idk?
std::cerr << "Exception in ThreadPool::join(): " << err.what() << '\n';
}
}
}
}
private:
/**
* Constantly running function each thread uses to acquire work items from the queue.
@ -194,17 +212,11 @@ namespace roar11
/**
* Invalidates the queue and joins all running threads.
*/
void destroy(void)
void destroy(void) noexcept
{
m_done = true;
m_workQueue.invalidate();
for(auto& thread : m_threads)
{
if(thread.joinable())
{
thread.join();
}
}
join();
}
private:

View file

@ -4,6 +4,7 @@
*/
#pragma once
#include <system_error>
#ifndef THREADSAFEQUEUE_HPP
#define THREADSAFEQUEUE_HPP
@ -12,6 +13,7 @@
#include <mutex>
#include <queue>
#include <utility>
#include <iostream>
namespace roar11
{
@ -107,11 +109,17 @@ namespace roar11
* The queue is invalid after calling this method and it is an error
* to continue using a queue after this method has been called.
*/
void invalidate(void)
void invalidate(void) noexcept
{
std::lock_guard<std::mutex> lock{m_mutex};
m_valid = false;
m_condition.notify_all();
try {
std::lock_guard<std::mutex> lock{m_mutex}; //<-- can throw
m_valid = false;
m_condition.notify_all();
}
catch (const std::system_error& err) {
std::cerr << "Error in ThreadSafeQueue::invalidate(): " <<
err.what() << '\n';
}
}
/**