Skip to content

added a TLS using example for task#77

Merged
dietmarkuehl merged 2 commits intomainfrom
tls-task
Mar 23, 2026
Merged

added a TLS using example for task#77
dietmarkuehl merged 2 commits intomainfrom
tls-task

Conversation

@dietmarkuehl
Copy link
Member

No description provided.

Copilot AI review requested due to automatic review settings March 23, 2026 20:33
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds a new example that demonstrates using a thread-local allocator with beman::execution::task, and wires it into the examples build.

Changes:

  • Add examples/alloc-2.cpp showcasing a TLS-backed allocator_type for tasks.
  • Register the new alloc-2 example target in examples/CMakeLists.txt.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
examples/alloc-2.cpp New allocator example using thread_local state to select a std::pmr::memory_resource for task allocations.
examples/CMakeLists.txt Adds alloc-2 to the list of built examples (non-MSVC).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@@ -0,0 +1,82 @@
// examples/alloc-1.cpp -*-C++-*-
Copy link

Copilot AI Mar 23, 2026

Choose a reason for hiding this comment

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

File header comment says examples/alloc-1.cpp but this is alloc-2.cpp, which can confuse readers and tooling that relies on the header banner. Update the comment to match the actual filename.

Suggested change
// examples/alloc-1.cpp -*-C++-*-
// examples/alloc-2.cpp -*-C++-*-

Copilot uses AI. Check for mistakes.
Comment on lines +30 to +31
auto p = std::malloc(n);
std::cout << " global new(" << n << ")->" << p << "\n";
Copy link

Copilot AI Mar 23, 2026

Choose a reason for hiding this comment

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

The throwing global operator new(std::size_t) is required to throw std::bad_alloc on allocation failure; returning nullptr is undefined behavior for callers that assume a valid pointer. Consider checking the malloc result and throwing, or implement only the nothrow overload if you intend to return null.

Suggested change
auto p = std::malloc(n);
std::cout << " global new(" << n << ")->" << p << "\n";
void* p = std::malloc(n);
std::cout << " global new(" << n << ")->" << p << "\n";
if (!p) {
throw std::bad_alloc();
}

Copilot uses AI. Check for mistakes.
Comment on lines +44 to +51
void* do_allocate(std::size_t n, std::size_t) override {
auto p{std::malloc(n)};
std::cout << " resource::allocate(" << n << ")->" << p << "\n";
return p;
}
void do_deallocate(void* p, std::size_t n, std::size_t) override {
std::cout << " resource::deallocate(" << p << ", " << n << ")\n";
std::free(p);
Copy link

Copilot AI Mar 23, 2026

Choose a reason for hiding this comment

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

std::pmr::memory_resource::do_allocate(bytes, alignment) must honor the requested alignment. Ignoring the alignment parameter and using malloc can return misaligned storage for over-aligned allocations. Use an aligned allocation strategy (e.g., ::operator new(bytes, std::align_val_t(alignment)) + matching aligned delete, or std::aligned_alloc where available).

Suggested change
void* do_allocate(std::size_t n, std::size_t) override {
auto p{std::malloc(n)};
std::cout << " resource::allocate(" << n << ")->" << p << "\n";
return p;
}
void do_deallocate(void* p, std::size_t n, std::size_t) override {
std::cout << " resource::deallocate(" << p << ", " << n << ")\n";
std::free(p);
void* do_allocate(std::size_t n, std::size_t alignment) override {
void* p = nullptr;
#if defined(__cpp_aligned_new)
if (alignment > __STDCPP_DEFAULT_NEW_ALIGNMENT__) {
p = ::operator new(n, std::align_val_t(alignment));
} else
#endif
{
p = ::operator new(n);
}
std::cout << " resource::allocate(" << n << ")->" << p << "\n";
return p;
}
void do_deallocate(void* p, std::size_t n, std::size_t alignment) override {
std::cout << " resource::deallocate(" << p << ", " << n << ")\n";
#if defined(__cpp_aligned_new)
if (alignment > __STDCPP_DEFAULT_NEW_ALIGNMENT__) {
::operator delete(p, n, std::align_val_t(alignment));
} else
#endif
{
::operator delete(p, n);
}

Copilot uses AI. Check for mistakes.
Comment on lines +77 to +80
tls_allocator::set(&res);
ex::sync_wait([ac]() -> a_task<> {
auto result{co_await async_fun(ac)};
std::cout << " result=" << result << "\n";
Copy link

Copilot AI Mar 23, 2026

Choose a reason for hiding this comment

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

tls_allocator::set(&res) stores a pointer to a stack resource in a thread_local that outlives res. Even though this example exits soon after, leaving a thread_local pointing at a soon-to-be-destroyed object is easy to copy/paste into real code and becomes a dangling pointer hazard. Consider resetting back to std::pmr::new_delete_resource() after the sync_wait, or wrap the change in an RAII guard object.

Copilot uses AI. Check for mistakes.
@coveralls
Copy link

coveralls commented Mar 23, 2026

Coverage Status

coverage: 94.212%. remained the same
when pulling ebc86d4 on tls-task
into 768ee35 on main.

@dietmarkuehl dietmarkuehl merged commit 6a58aff into main Mar 23, 2026
36 checks passed
@dietmarkuehl dietmarkuehl deleted the tls-task branch March 23, 2026 20:43
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.

3 participants