mirror of
https://gitlab.com/niansa/cosched.git
synced 2025-03-06 20:53:26 +01:00
Moved ScheduledThread::main_loop into another file
This commit is contained in:
parent
1e3e3c7786
commit
e53d6c2c7e
3 changed files with 28 additions and 23 deletions
|
@ -7,7 +7,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
|||
|
||||
add_subdirectory(libasync)
|
||||
|
||||
add_library(cosched STATIC scheduler.cpp include/scheduler.hpp include/scheduled_thread.hpp)
|
||||
add_library(cosched STATIC scheduler.cpp include/scheduler.hpp scheduled_thread.cpp include/scheduled_thread.hpp)
|
||||
target_link_libraries(cosched PUBLIC async)
|
||||
target_include_directories(cosched PUBLIC include/)
|
||||
|
||||
|
|
|
@ -20,28 +20,7 @@ class ScheduledThread {
|
|||
bool shutdown_requested = false;
|
||||
bool joined = false;
|
||||
|
||||
void main_loop() {
|
||||
// Create scheduler
|
||||
Scheduler sched;
|
||||
// Loop until shutdown is requested
|
||||
while (!shutdown_requested) {
|
||||
// Start all new tasks enqueued
|
||||
std::scoped_lock L(queue_mutex);
|
||||
while (!queue.empty()) {
|
||||
auto f = std::move(queue.front());
|
||||
queue.pop();
|
||||
f(sched);
|
||||
}
|
||||
// Run once
|
||||
sched.run_once();
|
||||
// Wait for work if there is none
|
||||
if (!sched.has_work()) {
|
||||
if (joined) break;
|
||||
std::unique_lock<std::mutex> lock(conditional_mutex);
|
||||
conditional_lock.wait(lock);
|
||||
}
|
||||
}
|
||||
}
|
||||
void main_loop();
|
||||
|
||||
public:
|
||||
ScheduledThread() {}
|
||||
|
|
26
scheduled_thread.cpp
Normal file
26
scheduled_thread.cpp
Normal file
|
@ -0,0 +1,26 @@
|
|||
#include "scheduled_thread.hpp"
|
||||
|
||||
|
||||
|
||||
void CoSched::ScheduledThread::main_loop() {
|
||||
// Create scheduler
|
||||
Scheduler sched;
|
||||
// Loop until shutdown is requested
|
||||
while (!shutdown_requested) {
|
||||
// Start all new tasks enqueued
|
||||
std::scoped_lock L(queue_mutex);
|
||||
while (!queue.empty()) {
|
||||
auto f = std::move(queue.front());
|
||||
queue.pop();
|
||||
f(sched);
|
||||
}
|
||||
// Run once
|
||||
sched.run_once();
|
||||
// Wait for work if there is none
|
||||
if (!sched.has_work()) {
|
||||
if (joined) break;
|
||||
std::unique_lock<std::mutex> lock(conditional_mutex);
|
||||
conditional_lock.wait(lock);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue