aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--example/demo1.cc1
-rw-r--r--lib/executor.cc15
-rw-r--r--lib/executor.h4
3 files changed, 11 insertions, 9 deletions
diff --git a/example/demo1.cc b/example/demo1.cc
index aa13c0c..1c910cd 100644
--- a/example/demo1.cc
+++ b/example/demo1.cc
@@ -27,7 +27,6 @@ int main() {
e.spawn(std::make_unique<TestThread>("Thread2"));
e.spawn(std::make_unique<TestThread>("Thread3"));
e.run();
- e.run();
puts("[main] finish main thread");
return 0;
diff --git a/lib/executor.cc b/lib/executor.cc
index af49f0e..8977ee3 100644
--- a/lib/executor.cc
+++ b/lib/executor.cc
@@ -6,16 +6,19 @@
namespace nMatcha {
void Executor::spawn(std::unique_ptr<Thread> t) {
- mThreads.push_back(std::move(t));
- mThreads.back()->mExecutor = this;
+ mThreads.push_front(std::move(t));
+ mThreads.front()->mExecutor = this;
}
void Executor::run() {
- for (const std::unique_ptr<Thread>& t : mThreads) {
- if (t->isFinished()) {
- continue;
+ while (!mThreads.empty()) {
+ for (const std::unique_ptr<Thread>& t : mThreads) {
+ if (!t->isFinished()) {
+ yield_to(t.get());
+ }
}
- yield_to(t.get());
+
+ mThreads.remove_if([](const std::unique_ptr<Thread>& t) { return t->isFinished(); });
}
}
diff --git a/lib/executor.h b/lib/executor.h
index 5d2e5b6..67493d4 100644
--- a/lib/executor.h
+++ b/lib/executor.h
@@ -4,8 +4,8 @@
#include "thread.h"
+#include <forward_list>
#include <memory>
-#include <vector>
namespace nMatcha {
struct Executor {
@@ -20,7 +20,7 @@ namespace nMatcha {
private:
void* mStackPtr;
- std::vector<std::unique_ptr<Thread>> mThreads;
+ std::forward_list<std::unique_ptr<Thread>> mThreads;
void yield_to(const Thread* t) const;
};