Block-Structured AMR Software Framework
Loading...
Searching...
No Matches
AMReX_BackgroundThread.H
Go to the documentation of this file.
1#ifndef AMREX_BACKGROUND_THREAD_H_
2#define AMREX_BACKGROUND_THREAD_H_
3#include <AMReX_Config.H>
4
5#include <algorithm>
6#include <condition_variable>
7#include <functional>
8#include <memory>
9#include <mutex>
10#include <queue>
11#include <thread>
12
18namespace amrex {
19
26{
27public:
40
45 void Submit (std::function<void()>&& a_f);
49 void Submit (std::function<void()> const& a_f);
50
54 void Finish ();
55
56private:
57 void do_job ();
58
59 std::unique_ptr<std::thread> m_thread;
60 std::mutex m_mutx;
61 std::condition_variable m_job_cond;
62 std::condition_variable m_done_cond;
63 std::queue<std::function<void()> > m_func;
64 bool m_clearing = false;
65 bool m_finalizing = false;
66};
67
68}
69
70#endif
Utility class running deferred work on a single background thread.
Definition AMReX_BackgroundThread.H:26
void Finish()
Block the caller until all queued jobs have completed.
Definition AMReX_BackgroundThread.cpp:52
BackgroundThread()
Launch the worker thread and initialize synchronization primitives.
Definition AMReX_BackgroundThread.cpp:5
void Submit(std::function< void()> &&a_f)
Enqueue a job that will run asynchronously on the worker thread.
Definition AMReX_BackgroundThread.cpp:38
~BackgroundThread()
Stop the worker thread and drain any queued work.
Definition AMReX_BackgroundThread.cpp:10
BackgroundThread & operator=(BackgroundThread const &)=delete
BackgroundThread(BackgroundThread const &)=delete
BackgroundThread(BackgroundThread &&)=delete
Definition AMReX_Amr.cpp:50