Skip to content

Conversation

@kimkulling
Copy link
Owner

@kimkulling kimkulling commented Nov 5, 2025

Summary by CodeRabbit

  • New Features
    • Added a lightweight C++ asynchronous task framework enabling task submission, execution control, status monitoring, and result retrieval.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 5, 2025

Walkthrough

Adds a new C++ header include/cppcore/Concurrent/async.h defining a lightweight async task API: AsyncStatus, context_t, Executor (task submission/control) and AsyncTask (task wrapper/start/get) in namespace cppcore::concurrent.

Changes

Cohort / File(s) Change Summary
New Async Header
include/cppcore/Concurrent/async.h
Adds AsyncStatus enum (Pending, Running, Completed, Failed); context_t with void* data; Executor class (ctor/dtor, non-copyable, run, isRunning, templated submit, shutdown); AsyncTask class (templated ctor, start with optional context_t, status(), templated get()).

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant Executor
  participant AsyncTask

  Caller->>Executor: submit(task)
  Note over Executor: store task, return
  Caller->>Executor: run()
  Executor->>AsyncTask: start(ctx)
  activate AsyncTask
  AsyncTask->>AsyncTask: execute function (possibly async)
  AsyncTask-->>Executor: status = Running/Completed/Failed
  deactivate AsyncTask
  Caller->>AsyncTask: get<ResultType>()
  Note right of Caller: blocks/returns result
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Pay attention to template method signatures and potential missing constraints
  • Verify semantics of default context_t in start() and lifetime of void* data
  • Confirm non-copyable Executor is intentional and resource management expectations

Poem

🐇 In fields of code I softly dart,

I spawn a task, then play my part.
Pending hops to running cheer,
Completed carrots drawing near,
A rabbit's clap — async is here! 🥕

Pre-merge checks and finishing touches

❌ Failed checks (1 inconclusive)
Check name Status Explanation Resolution
Title check ❓ Inconclusive The title 'First prototypes' is vague and generic, failing to convey specific information about the changeset which adds an asynchronous task framework. Use a more descriptive title that reflects the main change, such as 'Add async task framework with Executor and AsyncTask classes' or similar.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fwature/add_async

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 00ef0db and 01fdb5c.

📒 Files selected for processing (1)
  • include/cppcore/Concurrent/async.h (1 hunks)
🧰 Additional context used
🪛 Clang (14.0.6)
include/cppcore/Concurrent/async.h

[error] 3-3: unknown type name 'namespace'

(clang-diagnostic-error)


[error] 3-3: expected ';' after top level declarator

(clang-diagnostic-error)

Comment on lines +17 to +42
class Executor {
public:
Executor();
~Executor();
Executor(const Executor&) = delete;
Executor& operator=(const Executor&) = delete;

void run();

bool isRunning() const;

/**
* @brief Submits a task for asynchronous execution.
*
* @param task The task to be executed.
*/
template<typename Task>
void submit(Task&& task);

/**
* @brief Shuts down the executor, waiting for all tasks to complete.
*/
void shutdown();

private:
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Provide concrete definitions for Executor’s API.

run(), isRunning(), submit(), and shutdown() are only declared here, and the PR does not supply definitions anywhere else. The first translation unit that calls any of them will fail to link with unresolved externals. Either supply working implementations in this PR (inline or in a new .cpp) or make the class an abstract interface with pure-virtual members until the executor exists. Right now the header exports a non-functional type.

🤖 Prompt for AI Agents
In include/cppcore/Concurrent/async.h around lines 17 to 42, the Executor class
declares run(), isRunning(), submit(), and shutdown() but provides no
definitions, causing link errors; either implement them here (inline) or convert
the class into an abstract interface with pure-virtual methods. To fix: choose
implementation — add private members (std::thread worker_, std::mutex mu_,
std::condition_variable cv_, std::deque<std::function<void()>> queue_,
std::atomic<bool> running_{false}, std::atomic<bool> stopping_{false});
implement run() to set running_=true and start worker_ looping, waiting on cv_
to pop tasks and execute them until stopping_; implement submit(Task&& t) as a
templated inline method that wraps the task into std::function<void()>, pushes
into queue_ under lock, and notifies cv_; implement isRunning() to return
running_.load(); implement shutdown() to set stopping_=true, notify cv_, join
worker_, and set running_=false. Alternatively, if you prefer an abstract type,
mark run/isRunning/submit/shutdown as pure virtual (virtual ... = 0) and remove
any member-data to avoid exporting a non-functional concrete type.

Comment on lines +52 to +82
public:
/**
* @brief Constructs an AsyncTask with the given function.
*
* @param func The function to be executed asynchronously.
*/
template<typename Func>
explicit AsyncTask(Func&& func) {
// Implementation here
}

/**
* @brief Starts the asynchronous task.
*/
void start(const context_t& ctx = { nullptr });

/// @brief
/// @return
AsyncStatus status() const;

/**
* @brief Waits for the task to complete and retrieves the result.
*
* @return The result of the asynchronous task.
*/
template<typename ResultType>
ResultType get();

private:

};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Finish AsyncTask before publishing it.

AsyncTask exposes start, status, and get but provides no storage for the callable, no state tracking, and—most critically—no definitions for start()/status()/get() in this PR. Instantiating or calling these members will not compile (template get() has no body) or will be undefined behaviour. Please wire the constructor to capture the function, add the necessary state/result members, and implement the lifecycle methods (e.g., backing them with std::future/std::packaged_task). Until that’s in place, the class cannot be used.

🤖 Prompt for AI Agents
In include/cppcore/Concurrent/async.h around lines 52 to 82, AsyncTask currently
declares start(), status(), and template get() but lacks storage for the
callable, task state/result, and implementations; this prevents compilation and
causes UB. Fix by adding members to store the callable (type-erased via
std::function or templated member), a std::packaged_task<R()> or
std::promise/std::future pair to hold the result, an atomic AsyncStatus/state
flag and optional std::thread for execution, and implement the constructor to
capture/move the callable; implement start(const context_t&) to launch the
packaged_task on a new thread (or std::async) and set state, implement status()
to read the atomic state and map future validity to AsyncStatus, and implement
template get() to wait on the future and return the result (propagating
exceptions); ensure proper thread join/detach and move/copy semantics to avoid
races and double-starts.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

♻️ Duplicate comments (2)
include/cppcore/Concurrent/async.h (2)

39-64: Critical: Missing method definitions (previously reported).

The Executor class declares run(), isRunning(), submit(), and shutdown() but provides no definitions. This will cause linker errors when any of these methods are called. The previous review comment already covers this critical issue in detail.

Additional note: When you implement these methods, you'll need to add the necessary includes at the top of the file:

#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <deque>
#include <atomic>

85-100: Critical: Missing method definitions (previously reported).

The start(), status(), and get<ResultType>() methods are declared but not defined. The previous review comment at lines 74-104 already covers this critical issue and provides detailed guidance on implementation.

Minor documentation issue: The doxygen comment for status() at lines 90-91 is incomplete (empty @brief and @return tags).

-        /// @brief 
-        /// @return 
+        /// @brief Returns the current status of the asynchronous task.
+        /// @return The task's AsyncStatus (Pending, Running, Completed, or Failed).
         AsyncStatus status() const;
🧹 Nitpick comments (1)
include/cppcore/Concurrent/async.h (1)

35-37: Consider renaming and improving type safety.

The _t suffix is typically reserved for C types or POSIX types. In C++ namespaces, consider Context or ContextData instead. Additionally, the void* lacks type safety and provides no indication of ownership or lifetime semantics.

Consider these improvements:

-    struct context_t {
-        void *data;
+    /// @brief Execution context passed to async tasks.
+    struct Context {
+        void *data = nullptr;  // Or consider std::any for type-safety (C++17+)
     };

Then update usage throughout:

void start(const Context& ctx = {});
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 01fdb5c and cb0bac6.

📒 Files selected for processing (1)
  • include/cppcore/Concurrent/async.h (1 hunks)
🧰 Additional context used
🪛 Clang (14.0.6)
include/cppcore/Concurrent/async.h

[error] 25-25: unknown type name 'namespace'

(clang-diagnostic-error)


[error] 25-25: expected ';' after top level declarator

(clang-diagnostic-error)

🔇 Additional comments (3)
include/cppcore/Concurrent/async.h (3)

1-23: LGTM!

The MIT license header is properly formatted and complete. The use of #pragma once is acceptable for modern compilers.


28-33: LGTM!

The AsyncStatus enum class provides appropriate states for async task lifecycle management. Using enum class ensures type safety.


80-83: Remove or implement the empty constructor body.

The constructor template has an empty implementation body with just a placeholder comment. This is misleading because it compiles without error but is completely non-functional—it doesn't capture or store the func parameter. This is actually worse than leaving the method unimplemented (which would cause a linker error and fail fast).

Either remove the body entirely to get a linker error:

         template<typename Func>
-        explicit AsyncTask(Func&& func) {
-            // Implementation here
-        }
+        explicit AsyncTask(Func&& func);

Or add the necessary private members and implement it properly (per the previous detailed review comment at lines 74-104).

Likely an incorrect or invalid review comment.

@sonarqubecloud
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants