Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

Potential sync optimizations for single-threaded schedulers

Abierto
#1,471 3 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
5/5
Tiempo estimado
Más de una semana
Aptitud para principiantes
25/100
Tipo de issue
Nueva funcionalidad
Claridad
Necesita aclaración
Estado de actividad
Estancado
Stack tecnológico
cpp
Área
backend

Línea de trabajo

Comienza reproduciendo el ejemplo de Godbolt y leyendo las implementaciones de sync_wait, async_scope e inline_scheduler mencionadas en el issue. Rastrea dónde se introducen los mutexes y las variables de condición, y si allí está disponible la información del tipo de scheduler. Se considera completado cuando se haya establecido si la sincronización en un solo hilo puede eliminarse de forma segura y se haya documentado una dirección concreta para la implementación, o el motivo por el que el diseño lo impide.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

I've been playing around with stdexec and had a thought about sync_wait and async_scope that I wanted to run by the community. I might be missing something obvious here, so please let me know if this doesn't make sense!

I noticed that when using sync_wait with inline_scheduler, we still use mutexes and condition variables internally, even though we know the work will run on the calling thread. Same thing seems to happen with async_scope when all tasks run on a single thread.

I was wondering:

  1. Is there a way (or maybe there already is?) to optimize away this synchronization overhead when we know at compile-time that everything will run on the same thread?
  2. Does the sender type system provide enough information to detect these cases?

Quick example of what made me think about this (godbolt):

#include <stdexec/execution.hpp>
#include <exec/inline_scheduler.hpp>

int main(int argc, char* argv[]) {
  auto sdr = stdexec::schedule(exec::inline_scheduler{})
    | stdexec::then([argc] { return argc; })
    | stdexec::then([](auto n) { return 2 * n; });
    
  auto [res] = stdexec::sync_wait(std::move(sdr)).value();

  return res;
}

I expected the generated assembly to be equivalent, or extremely close, to simply writing:

int main(int argc, char* argv[]) {
    auto then_f = [](auto n) { return 2 * n; };
    auto [res] = std::optional{std::tuple{std::move(then_f)(argc)}}.value();
    return res;
}

However, the generated code uses mutexes and condition variables.

I was thinking: could sync_wait potentially use type traits from the active scheduler to determine if it needs to be thread-safe? That way, it could avoid mutexes and condition variables in single-threaded cases.

The same could apply to async_scopes that run in single-threaded environments.

If my understanding of sync_wait and inline_scheduler is incorrect, please let me know!
Otherwise, it would be interesting to know whether such overhead is avoidable in single-threaded cases or if it's inherently tied to the design.

Thanks for any insights!

Lenguaje dominante
C++
Estrellas
2.4k
Forks
270
Merge medio
2 d 17 h
PR fusionados (30 d)
37

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de NVIDIA/stdexec

Todos los issues de NVIDIA/stdexec

Issues similares

Más issues de C++

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.