Skip to content

Commit

Permalink
avoid using std::thread for top-level async support in helide (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffamstutz authored Jan 26, 2024
1 parent 6faef14 commit aecbcd4
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
11 changes: 4 additions & 7 deletions libs/helide/frame/Frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <algorithm>
#include <chrono>
#include <random>
#include <thread>
// embree
#include "algorithms/parallel_for.h"

Expand Down Expand Up @@ -38,13 +37,11 @@ static void serial_for(I size, FUNC &&f)
}

template <typename R, typename TASK_T>
static std::future<R> async(TASK_T &&fcn)
static std::future<R> async(std::packaged_task<R()> &task, TASK_T &&fcn)
{
auto task = std::packaged_task<R()>(std::forward<TASK_T>(fcn));
task = std::packaged_task<R()>(std::forward<TASK_T>(fcn));
auto future = task.get_future();

std::thread([task = std::move(task)]() mutable { task(); }).detach();

embree::TaskScheduler::spawn([&]() { task(); });
return future;
}

Expand Down Expand Up @@ -146,7 +143,7 @@ void Frame::renderFrame()
state->waitOnCurrentFrame();
state->currentFrame = this;

m_future = async<void>([&, state]() {
m_future = async<void>(m_task, [&, state]() {
auto start = std::chrono::steady_clock::now();
state->renderingSemaphore.frameStart();
state->commitBufferFlush();
Expand Down
1 change: 1 addition & 0 deletions libs/helide/frame/Frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ struct Frame : public helium::BaseFrame
helium::TimeStamp m_frameLastRendered{0};

mutable std::future<void> m_future;
std::packaged_task<void()> m_task;
};

} // namespace helide
Expand Down

0 comments on commit aecbcd4

Please sign in to comment.