Académique Documents
Professionnel Documents
Culture Documents
#include <thread>
#include <string>
#include <optional>
#include <vector>
#include <mutex>
#include <algorithm>
namespace Dinner::Concurrency
{
extern std::mutex thread_list_mutex;
public: //ctor
named_thread() noexcept = default;
private:
std::string name_;
};
public://interface
void stop() override
{
running_ = false;
}
public: //ctor
poll_thread() noexcept = default;
private:
std::unique_ptr<function_info_t> function_info_;
bool running_;
while (running_)
{
auto& params = *function_info_;
std::get<std::decay_t<Fn>>(params)
(std::get<std::decay_t<Args>>(params)...);
}
}
};
std::optional<std::reference_wrapper<named_thread>> find_thread(const
std::string& thread_name);
std::optional<std::reference_wrapper<named_thread>>
find_thread(std::thread::id thread_id);
std::optional<std::reference_wrapper<named_thread>> current_thread();
std::size_t thread_count();
namespace detail
{
template <typename ThreadType, typename Fn, typename... Args>
void launch_impl(const std::string& name, Fn&& fn, Args&&... args)
{
const auto thread_stub = [](auto&& fn, auto&&... args)
{
// Invoke the given function.
fn(std::forward<decltype(args)>(args)...);
return false;
});
if (end_range != detail::threads.end())
{
// First detach the current thread from the thread
list so that
// the current thread isn't terminated prematurely.
(*end_range)->detach();
detail::threads.emplace_back(std::make_unique<ThreadType>(
name,
thread_stub,
std::forward<Fn>(fn),
std::forward<Args>(args)...
));
}
}