Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions include/cppcore/Concurrent/async.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*-----------------------------------------------------------------------------------------------
The MIT License (MIT)

Copyright (c) 2014-2025 Kim Kulling

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-----------------------------------------------------------------------------------------------*/
#pragma once

namespace cppcore {
namespace concurrent {

enum class AsyncStatus {
Pending,
Running,
Completed,
Failed
};

struct context_t {
void *data;
};

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:
};
Comment on lines +39 to +64
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.


/**
* @brief Asynchronous task representation.
*
* This class provides a mechanism to represent and manage asynchronous tasks.
* It allows for task creation, execution, and result retrieval in a concurrent
* environment.
*/
class AsyncTask {
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:

};
Comment on lines +74 to +104
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.


} // namespace concurrent
} // namespace cppcore