Minimal example for spawning NUM_CPU
threads and running an app instance on each?
#674
Answered
by
npuichigo
GavinRay97
asked this question in
Q&A
-
Hiya, I'm curious what the rough approach would be for doing something like this:
I think the rough outline for something like this would be: struct MyApplication
{
void run()
{
while (true) std::this_thread::yield();
}
}
int main()
{
auto num_cpus = std::thread::hardware_concurrency();
exec::static_thread_pool pool(num_cpus);
auto sched = pool.get_scheduler();
std::vector<std::jthread> threads;
for (auto i = 0; i < num_cpus; ++i)
{
threads.emplace_back([sched]()
{
auto exec = std::execution::require(sched, std::execution::never_blocking);
MyApplication app;
std::execution::execute(exec, &app, &MyApplication::run);
});
}
for (auto& t : threads)
{
t.join();
}
} Thank you =) |
Beta Was this translation helpful? Give feedback.
Answered by
npuichigo
Nov 4, 2022
Replies: 1 comment 2 replies
-
Something like this? int main()
{
auto num_cpus = std::thread::hardware_concurrency();
exec::static_thread_pool pool(num_cpus);
auto make_snd = [&]() {
return ex::schedule(pool.get_scheduler())
| ex::then([]{
MyApplication app;
app.run();
});
}
auto snd = ex::when_all(
make_snd(),
make_snd(),
make_snd()
)
stdexec::sync_wait(std::move(snd));
} |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
GavinRay97
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Something like this?